Row names and column names in R

Do the following pairs of functions perform the same results?

Pair 1) names() and colnames()

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

+41
r rowname
Feb 17 '10 at 14:20
source share
4 answers

As Oscar Wilde said

Consistency is the last prosaic refuge.

R is more developed than developed language, so everything happens. names() and colnames() work with data.frame , but names() does not work on the 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> 
+53
Feb 17 '10 at 02:53
source share

To talk a bit about the Dirk example:

This helps to think of a data frame as a list with vectors of equal length. This is probably why names works with the data frame, but not with the matrix.

Another useful feature is dimnames , which returns names for each dimension. You will notice that the rownames function actually returns the first element from dimnames .

Regarding rownames and row.names : I can't tell the difference, although rownames uses dimnames and row.names was written outside R. They both seem to work with arrays with a higher dimension:

 >a <- array(1:5, 1:4) > a[1,,,] > rownames(a) <- "a" > row.names(a) [1] "a" > a , , 1, 1 [,1] [,2] a 1 2 > dimnames(a) [[1]] [1] "a" [[2]] NULL [[3]] NULL [[4]] NULL 
+8
Feb 17 '10 at 15:09
source share

I think using colnames and rownames makes the most sense; that's why.

Using names has several drawbacks. You must remember that this means “column names” and it only works with the data frame, so you will need to call colnames whenever you use matrices. When calling colnames , you only need to remember one function. Finally, if you look at the code for colnames , you will see that it calls names in the case of a data frame anyway, so the output is identical.

rownames and row.names return the same values ​​for the data frame and matrices; the only difference I noticed is that where there are no names, rownames will print "NULL" (like colnames ), but row.names returns it invisibly. Since there is not much choice between the two functions, rownames wins based on aesthetics, as it combines better with colnames . (Also, for a lazy programmer, you keep the character of the input.)

+5
Feb 17 '10 at 15:35
source share

And one more extension:

 # create dummy matrix set.seed(10) m <- matrix(round(runif(25, 1, 5)), 5) d <- as.data.frame(m) 

If you want to assign new column names, you can do the following on data.frame :

 # an identical effect can be achieved with colnames() names(d) <- LETTERS[1:5] > d ABCDE 1 3 2 4 3 4 2 2 2 3 1 3 3 3 2 1 2 4 4 4 3 3 3 2 5 1 3 2 4 3 

If you, however, run the previous command on matrix , you will ruin everything:

 names(m) <- LETTERS[1:5] > m [,1] [,2] [,3] [,4] [,5] [1,] 3 2 4 3 4 [2,] 2 2 3 1 3 [3,] 3 2 1 2 4 [4,] 4 3 3 3 2 [5,] 1 3 2 4 3 attr(,"names") [1] "A" "B" "C" "D" "E" NA NA NA NA NA NA NA NA NA NA NA NA NA NA [20] NA NA NA NA NA NA 

Since the matrix can be thought of as a two-dimensional vector, you only give names to the first five values ​​(you don't want to do this, do you?). In this case, you should stick with colnames() .

So there ...

+2
Feb 17 '10 at 18:40
source share



All Articles