Row names & column names in R

后端 未结 4 2185
借酒劲吻你
借酒劲吻你 2020-12-23 19:31

Do the following function pairs generate exactly the same results?

Pair 1) names() & colnames()

Pair 2) rownames()

4条回答
  •  春和景丽
    2020-12-23 20:07

    Just to expand a little on Dirk's example:

    It helps to think of a data frame as a list with equal length vectors. That's probably why names works with a data frame but not a matrix.

    The other useful function is dimnames which returns the names for every dimension. You will notice that the rownames function actually just returns the first element from dimnames.

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

    >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
    

提交回复
热议问题