Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row names & column names in R

Do the following function pairs generate exactly the same results?

Pair 1) names() & colnames()

Pair 2) rownames() & row.names()

like image 726
Mehper C. Palavuzlar Avatar asked Feb 17 '10 14:02

Mehper C. Palavuzlar


People also ask

What are row names?

Description. All data frames have a row names attribute, a character vector of length the number of rows with no duplicates nor missing values. For convenience, these are generic functions for which users can write other methods, and there are default methods for arrays.

What does row names do in R?

The rownames() and colnames() functions in R are used to obtain or set the names of the row and column of a matrix-like object, respectively.

What does row names 1 do in R?

Assigning the second argument, row. names , to be 1 indicates that the data file has row names, and which column number they are stored in. If we don't specify row. names the result will not have row names.

How do I assign row names in R?

A data frame's rows can be accessed using rownames() method in the R programming language. We can specify the new row names using a vector of numerical or strings and assign it back to the rownames() method. The data frame is then modified reflecting the new row names.


1 Answers

As Oscar Wilde said

Consistency is the last refuge of the unimaginative.

R is more of an evolved rather than designed language, so these things happen. names() and colnames() work on a data.frame but names() does not work on a matrix:

R> DF <- data.frame(foo=1:3, bar=LETTERS[1:3]) R> names(DF) [1] "foo" "bar" R> colnames(DF) [1] "foo" "bar" R> M <- matrix(1:9, ncol=3, dimnames=list(1:3, c("alpha","beta","gamma"))) R> names(M) NULL R> colnames(M) [1] "alpha" "beta"  "gamma" R>  
like image 87
Dirk Eddelbuettel Avatar answered Sep 20 '22 00:09

Dirk Eddelbuettel