2015-02-05 1 views
2

각 구성원이 4 * 2 행렬 인 목록 (100 개 구성원)이 있습니다. R에서 루프를 사용하여이 목록을 차원 400 * 2의 배열로 변환하는 방법?동일한 순서의 행렬 목록을 배열로 변환

 x   y 
    85.56384 27.97745 
    85.58448 28.02133 
    85.60252 27.96366 
    85.62318 28.00753 
     :  : 
    85.58448 28.02133 
    85.60500 28.06502 
    85.62317 28.00754 
    85.64372 28.05122 

감사합니다 - 예를

A<-list() 


A[[1]] 
     x   y 
    85.56384 27.97745 
    85.58448 28.02133 
    85.60252 27.96366 
    85.62318 28.00753 

등 내가 <을 갖고 싶어 마지막으로

A[[100]] 
     x   y 
    85.58448 28.02133 
    85.60500 28.06502 
    85.62317 28.00754 
    85.64372 28.05122 

를 들어

도움을 청합니다.

+0

재현 예제를 추가하십시오. –

+0

가능한 [R에서 목록을보다 효율적으로 매트릭스로 변환하는 방법]? (http://stackoverflow.com/questions/13224553/how-to-convert-a-list-to-a-matrix-more- 효율적으로 in-r) –

+1

나는'data.table' 패키지의'rbindlist' 또는'tidyr' 패키지의'unnest'를 제안 할 것입니다. 보세요 [여기] (http://stackoverflow.com/questions/28135192/converting-list-of-data-frames-to-a-data-table/), 예를 들어 –

답변

6

Reduce가 여기에 제안 된 다양한 전략의 벤치 마크 테스트입니다 마십시오. 새로운 아이디어/전략이 있으면 언제든지 업데이트하십시오.

# packages 
require(data.table) 
require(tidyr) 
require(microbenchmark) 
# data 
lst <- replicate(100, matrix(rnorm(16), ncol=4), simplify=FALSE) 
# benchmark test 
microbenchmark(
    do.call(rbind, lst) 
    , 
    Reduce(rbind, lst) 
    , 
    apply(simplify2array(lst), 2, rbind) 
    , 
    rbindlist(lapply(lst, data.frame)) 
    , 
    unnest(lapply(lst, data.frame)) 
) 

결과 :

Unit: microseconds 
           expr  min   lq  mean  median  uq  max neval 
        do.call(rbind, lst) 43.290 47.9760 55.63858 52.8845 62.703 101.307 100 
        Reduce(rbind, lst) 542.236 570.7985 620.99652 585.3020 610.518 1871.272 100 
apply(simplify2array(lst), 2, rbind) 311.061 345.2010 382.22978 368.6315 388.268 1563.782 100 
    rbindlist(lapply(lst, data.frame)) 11827.884 12472.3190 13092.57937 12823.0995 13595.841 15833.736 100 
     unnest(lapply(lst, data.frame)) 12371.905 12927.9765 13514.24261 13236.1360 14008.655 16121.143 100 

그냥 호기심은 나뿐만 아니라 data.frame 입력에 대해 이러한 벤치 마크 테스트를 수행하고이 그림은 매우 다르다 :

# packages 
require(data.table) 
require(tidyr) 
require(microbenchmark) 
# data 
lst <- replicate(100, as.data.frame(matrix(rnorm(16), ncol=4)), simplify=FALSE) 
# benchmark test 
microbenchmark(
    do.call(rbind, lst) 
    , 
    Reduce(rbind, lst) 
    , 
    apply(simplify2array(lapply(lst, as.matrix)), 2, rbind) 
    , 
    rbindlist(lst) 
    , 
    unnest(lst) 
) 

와 결과

Unit: microseconds 
                expr  min   lq  mean median  uq  max neval 
            do.call(rbind, lst) 12406.716 12944.2660 13746.8552 13571.966 14564.056 16333.128 100  
             Reduce(rbind, lst) 36316.866 38450.7765 39894.9806 39299.610 40325.395 100949.158 100  
apply(simplify2array(lapply(lst, as.matrix)), 2, rbind) 9577.717 9940.9930 10273.8674 10065.059 10291.996 12114.846 100  
              rbindlist(lst) 324.896 369.0770 397.7828 402.995 426.202 500.732 100 
              unnest(lst) 926.487 974.9095 1011.7322 1010.834 1033.596 1171.051 100 
2

당신은 Reduce를 사용할 수 있습니다

> lst=list(matrix(1:16, ncol=4), matrix(4:23, ncol=4)) 
[[1]] 
    [,1] [,2] [,3] [,4] 
[1,] 1 5 9 13 
[2,] 2 6 10 14 
[3,] 3 7 11 15 
[4,] 4 8 12 16 

[[2]] 
    [,1] [,2] [,3] [,4] 
[1,] 4 9 14 19 
[2,] 5 10 15 20 
[3,] 6 11 16 21 
[4,] 7 12 17 22 
[5,] 8 13 18 23 

> Reduce(rbind, lst) 
     [,1] [,2] [,3] [,4] 
[1,] 1 5 9 13 
[2,] 2 6 10 14 
[3,] 3 7 11 15 
[4,] 4 8 12 16 
[5,] 4 9 14 19 
[6,] 5 10 15 20 
[7,] 6 11 16 21 
[8,] 7 12 17 22 
[9,] 8 13 18 23 

이 @Roland 제안에 따라 경찰, :)

#> lst=lapply(1:100000, function(u) m+u) 
#> system.time(do.call(rbind, lst)) 
# user system elapsed 
# 0.37 0.01 0.39 
#> system.time(Reduce(rbind, lst)) 
# user system elapsed 
# 704.94 38.66 743.96 
+1

'do.call (rbind, lst)'가 더 효율적이어야합니다. – Roland

+0

Reduce보다 왜 더 효율적인 지 설명 할 수 있습니까? (나는 당신을 신뢰하지만 이유를 알고 싶다.) –

+0

'Reduce'은'rbind'를 n-1 번,'do.call'을 한 번만 호출 할 것이다. 나는 그것을 벤치마킹하지 않았지만, 나는 더 효율적이라고 기대한다. – Roland

관련 문제