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

前端 未结 6 1095
再見小時候
再見小時候 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 02:16

    data.frame() can be used to build a data frame while as.data.frame() can only be used to coerce other object to a data frame.

    for example:

    # data.frame()
    df1 <- data.frame(matrix(1:12,3,4),1:3)
    
    # as.data.frame()
    df2 <- as.data.frame(matrix(1:12,3,4),1:3)
    
    df1
    #   X1 X2 X3 X4 X1.3
    # 1  1  4  7 10    1
    # 2  2  5  8 11    2
    # 3  3  6  9 12    3
    
    df2
    #   V1 V2 V3 V4
    # 1  1  4  7 10
    # 2  2  5  8 11
    # 3  3  6  9 12
    

提交回复
热议问题