rdataframematrixr-rownames

Why do row names disappear after as.matrix?


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?


Solution

  • 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.

    as.matrix(test)[c(1, 3), ]
    #        x   y
    # [1,] 0.1 0.1
    # [2,] 1.0 0.3
    

    WHAT's going on while using rownames is the dimnames feature in the serene source code of base:::rownames(),

    function (x, do.NULL = TRUE, prefix = "row") 
    {
      dn <- dimnames(x)
      if (!is.null(dn[[1L]])) 
        dn[[1L]]
      else {
        nr <- NROW(x)
        if (do.NULL) 
          NULL
        else if (nr > 0L) 
          paste0(prefix, seq_len(nr))
        else character()
      }
    }
    

    which yields NULL for dimnames(as.matrix(test))[[1]] but yields "1" "3" in the case of dimnames(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.