Row names & column names in R

Mehper C. Palavuzlar picture Mehper C. Palavuzlar · Feb 17, 2010 · Viewed 195.3k times · Source

Do the following function pairs generate exactly the same results?

Pair 1) names() & colnames()

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

Answer

Dirk Eddelbuettel picture Dirk Eddelbuettel · Feb 17, 2010

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>