Do the following function pairs generate exactly the same results?
Pair 1) names()
& colnames()
Pair 2) rownames()
& 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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With