2014-02-05 16 views
24

是什麼(X)差(x)的

as.data.frame(x)和data.frame之間的差異在下面的例子中,結果與列名稱的例外情況相同。

x <- matrix(data=rep(1,9),nrow=3,ncol=3) 
> x 
    [,1] [,2] [,3] 
[1,] 1 1 1 
[2,] 1 1 1 
[3,] 1 1 1 
> data.frame(x) 
    X1 X2 X3 
1 1 1 1 
2 1 1 1 
3 1 1 1 
> as.data.frame(x) 
    V1 V2 V3 
1 1 1 1 
2 1 1 1 
3 1 1 1 
+0

參見手冊:[as.data.frame](http://stat.ethz.ch/R-manual/R-devel /library/base/html/as.data.frame.html)和[data.frame](http://stat.ethz.ch/R-manual/R-devel/library/base/html/data.frame。 html) – zx8754

+8

若要進一步混淆事情,請嘗試'as(x,「data.frame」)'。 –

回答

11

正如夏侯,data.frame()調用as.data.frame(),但有它的理由中提到:

as.data.frame()是脅迫其他物體data.frame類的方法。如果您正在編寫自己的軟件包,則會存儲您的方法以便將your_class的對象轉換爲as.data.frame.your_class()。這裏只是一些例子。

methods(as.data.frame) 
[1] as.data.frame.AsIs   as.data.frame.Date   
[3] as.data.frame.POSIXct   as.data.frame.POSIXlt   
[5] as.data.frame.aovproj*  as.data.frame.array   
[7] as.data.frame.character  as.data.frame.complex   
[9] as.data.frame.data.frame  as.data.frame.default   
[11] as.data.frame.difftime  as.data.frame.factor   
[13] as.data.frame.ftable*   as.data.frame.integer   
[15] as.data.frame.list   as.data.frame.logLik*   
[17] as.data.frame.logical   as.data.frame.matrix   
[19] as.data.frame.model.matrix as.data.frame.numeric   
[21] as.data.frame.numeric_version as.data.frame.ordered   
[23] as.data.frame.raw    as.data.frame.table   
[25] as.data.frame.ts    as.data.frame.vector   

    Non-visible functions are asterisked 
7

正如你提到的,結果還是略有不同,這意味着它們並不完全等於:

identical(data.frame(x),as.data.frame(x)) 
[1] FALSE 

所以,你可能需要照顧到在你使用哪一個一致。

但是,這也是值得注意的是,as.data.frame更快:

library(microbenchmark) 
microbenchmark(data.frame(x),as.data.frame(x)) 
Unit: microseconds 
      expr min  lq median  uq  max neval 
    data.frame(x) 71.446 73.616 74.80 78.9445 146.442 100 
as.data.frame(x) 25.657 27.631 28.42 29.2100 93.155 100 

y <- matrix(1:1e6,1000,1000) 
microbenchmark(data.frame(y),as.data.frame(y)) 
Unit: milliseconds 
      expr  min  lq median  uq  max neval 
    data.frame(y) 17.23943 19.63163 23.60193 41.07898 130.66005 100 
as.data.frame(y) 10.83469 12.56357 14.04929 34.68608 38.37435 100 
1

嘗試

> colnames(x)<-c("C1","C2","C3") 

,然後兩者都會產生相同的結果

> identical(data.frame(x), as.data.frame(x)) 

什麼是更令人吃驚的是像下面這樣的東西:

> list(x) 

提供一個elemnt列表,elemnt是矩陣x;而

as.list(x) 

給出具有9個元素,每個矩陣條目列表

MM

6

data.frame()可以用來建立一個數據幀,而as.data.frame()只能用來強迫其他對象到一個數據幀。

例如:

> # 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 
+1

不一定。試試'is.object(1:5); as.data.frame(1:5)' –

0

看代碼,as.data.frame失敗得更快。 data.frame將發出警告,並且做這樣的事情,如果有重複刪除rownames:

> 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