I notice that if the row names of the dataframe follows a sequence of numbers from 1 to the number of rows. The row names of the dataframe will disappear after using as.matrix
. But the row names re-appear if the row name is not a sequence.
Here are a reproducible example:
test <- as.data.frame(list(x=c(0.1, 0.1, 1), y=c(0.1, 0.2, 0.3)))
rownames(test)
# [1] "1" "2" "3"
rownames(as.matrix(test))
# NULL
rownames(as.matrix(test[c(1, 3), ]))
# [1] "1" "3"
Why does this happen?
First and foremost, we always have a numerical index for sub-setting that won't disappear and that we should not confuse with row names.
WHAT's going on while using
rownames
is thedimnames
feature in the serene source code ofbase:::rownames()
,which yields
NULL
fordimnames(as.matrix(test))[[1]]
but yields"1" "3"
in the case ofdimnames(as.matrix(test[c(1, 3), ]))[[1]]
.Note, that the method
base:::row.names.data.frame
is applied in case of data frames, e.g.rownames(test)
.The WHAT should be explained with it, fortunately you did not ask for the WHY, which would be rather opinion-based.