Difference between as.data.frame(x) and data.frame(x)

前端 未结 6 1037
再見小時候
再見小時候 2020-12-24 01:44

What is the difference between as.data.frame(x) and data.frame(x)

In this following example, the result is the same at the exception of the columns names.

         


        
6条回答
  •  生来不讨喜
    2020-12-24 01:58

    Looking at the code, as.data.frame fails faster. data.frame will issue warnings, and do things like remove rownames if there are duplicates:

    > x <- matrix(data=rep(1,9),nrow=3,ncol=3)
    > rownames(x) <- c("a", "b", "b")
    > data.frame(x)
      X1 X2 X3
    1  1  1  1
    2  1  1  1
    3  1  1  1
    Warning message:
    In data.row.names(row.names, rowsi, i) :
      some row.names duplicated: 3 --> row.names NOT used
    
    > as.data.frame(x)
    Error in (function (..., row.names = NULL, check.rows = FALSE, check.names =        
    TRUE,  : 
      duplicate row.names: b
    

提交回复
热议问题