Row names & column names in R

后端 未结 4 2198
借酒劲吻你
借酒劲吻你 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 19:47

    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> 
    

提交回复
热议问题