2014-02-05 15 views
24

Was ist der Unterschied zwischen as.data.frame (x) und data.frame (x)Unterschied zwischen as.data.frame (x) und data.frame (x)

In diesem folgende Beispiel wird die Das Ergebnis ist das gleiche bei der Ausnahme der Spaltennamen.

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

Siehe Handbücher: [as.data.frame] (http://stat.ethz.ch/R-manual/R-devel /library/base/html/as.data.frame.html) vs [data.frame] (http://stat.ethz.ch/R-manual/R-devel/library/base/html/data.frame. html) – zx8754

+8

Um die Dinge noch weiter zu verwechseln, versuchen Sie 'as (x," data.frame ")'. –

Antwort

11

Wie von Jaap erwähnt, data.frame()as.data.frame() nennt, aber es gibt einen Grund dafür:

as.data.frame() ein Verfahren ist, andere Objekte zu Klasse data.frame zu zwingen. Wenn Sie Ihr eigenes Paket schreiben, würden Sie Ihre Methode speichern, um ein Objekt von your_class unter as.data.frame.your_class() zu konvertieren. Hier sind nur ein paar Beispiele.

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

Wie Sie bemerkt, unterscheidet sich das Ergebnis leicht, und dies bedeutet, dass sie nicht genau gleich sind:

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

Sie könnten also darauf achten, müssen Sie in die man konsequent sein verwenden.

Aber es ist auch erwähnenswert, dass as.data.frame ist schneller:

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

Versuchen

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

und dann werden beide das gleiche Ergebnis

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

Was erschreckend ist sind Dinge wie die folgenden:

> list(x) 

Stellt eine Ein-Element-Liste zur Verfügung, wobei das Element die Matrix x ist; während

as.list(x) 

gibt eine Liste mit 9 Elementen, eines für jeden Matrixeintrag

MM

6

data.frame() kann verwendet werden, um einen Datenrahmen zu erstellen, während as.data.frame() nur verwendet werden, kann anderes Objekt zu einem zwingen Datenrahmen.

zum Beispiel:

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

Nicht unbedingt. Probiere 'is.object (1: 5); as.data.frame (1: 5) ' –

0

auf dem Code der Suche, as.data.frame nicht schneller. data.frame werden Warnungen ausgeben, und Dinge tun, wie rownames zu entfernen, wenn es Duplikate sind:

> 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 
Verwandte Themen