2014-02-05 14 views
24

이 다음 예에서 as.data.frame (x)와 data.frame 차이 (X)사이의 차이 (x)는

이란 상기 결과는 열 이름을 제외하고는 동일합니다.

as.data.frame()를 호출하지만 이에 대한 이유가 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) vs [data.frame] (http://stat.ethz.ch/R-manual/R-devel/library/base/html/data.frame). html) – zx8754

+8

더 많은 것을 혼동하게하려면'as (x, "data.frame")'을 사용하십시오. –

답변

11

:

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) 

행렬 x가 elemnt 인 단일 전자 목록을 제공합니다.

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