R中按位置列表的平均值

时间:2017-05-02 02:39:56

标签: r

set.seed(0)
ESS <- list()

for (j in 1:100) {
    temp <- data.frame(v1 = rnorm(1000),
                       v2 = rnorm(1000), 
                       v3 = rnorm(1000)); 
    ESS[[j]] <- temp
}

我想将列表缩减为单个数据框,其中此数据框中的每个元素都是列表中存储的100个值的平均值。

4 个答案:

答案 0 :(得分:2)

由于您已经在ESS中编写了100个data.frames,因此ldply将返回每个data.frame的colMeans并将它们绑定到单个输出中:

library(plyr)
ldply(ESS, colMeans) 

答案 1 :(得分:1)

你是平均后的平均值吗?如果是,请尝试

df = data.frame(
  v1 = mean(as.numeric(lapply(ESS, function(x) mean(x$v1)))),
  v2 = mean(as.numeric(lapply(ESS, function(x) mean(x$v2)))),
  v3 = mean(as.numeric(lapply(ESS, function(x) mean(x$v3)))))

答案 2 :(得分:1)

我们可以使用lapply/sapply

中的base R
t(sapply(ESS, colMeans))
#                  v1            v2            v3
#  [1,] -0.0158295727 -2.478644e-02  0.0681427318
#  [2,]  0.0024859612  4.103752e-03  0.0197281287
#  [3,] -0.0020348399  4.206254e-02 -0.0488636326
#  [4,]  0.0594666004 -1.650206e-02 -0.0285346995
#  [5,] -0.0304168268  5.218406e-02  0.0146802682
#  [6,]  0.0349319791  3.032825e-03 -0.0278387255
#  [7,] -0.0237927055 -2.391049e-02  0.0304086913
#  [8,]  0.0125024040 -3.916709e-02  0.0089153103
#  [9,]  0.0061100088  2.059120e-02 -0.0262246088
# [10,] -0.0297768058  9.796194e-02 -0.0117578556
# [11,]  0.0061667120  1.629774e-02 -0.0108955127
# [12,]  0.0260390722 -5.002157e-02  0.0376300551
# [13,]  0.0190489482 -1.323166e-03 -0.0075387664
# [14,] -0.0205836198 -3.337794e-02 -0.0540212623
# [15,] -0.0187941477 -2.147293e-02 -0.0099970279
# [16,] -0.0398401942  5.897226e-02  0.0073399025
#    - - -
#    - - -
#[94,] -0.0039737458  2.082807e-02 -0.0315866491
# [95,]  0.0116898228 -2.924846e-02  0.0476551466
# [96,]  0.0331954808 -2.438589e-02  0.0006513433
# [97,]  0.0042186728  1.239381e-03  0.0102184203
# [98,] -0.0475462731  6.951947e-03  0.0315259709
# [99,] -0.0045903832 -5.146346e-03 -0.0426402874
#[100,] -0.0006757683 -4.334657e-02  0.0113538787

如果这是基于排名,那么我们可以使用Reduce

中的base R
newDat <- Reduce(`+`, ESS)/length(ESS)

答案 3 :(得分:1)

使用purrr:

library(purrr)

ESS %>% map_df(~as.list(colSums(.x)))

#> # A tibble: 100 × 3
#>            v1         v2        v3
#>         <dbl>      <dbl>     <dbl>
#> 1  -15.829573 -24.786443  68.14273
#> 2    2.485961   4.103752  19.72813
#> 3   -2.034840  42.062543 -48.86363
#> 4   59.466600 -16.502055 -28.53470
#> 5  -30.416827  52.184059  14.68027
#> 6   34.931979   3.032825 -27.83873
#> 7  -23.792706 -23.910489  30.40869
#> 8   12.502404 -39.167088   8.91531
#> 9    6.110009  20.591199 -26.22461
#> 10 -29.776806  97.961942 -11.75786
#> # ... with 90 more rows

或dplyr:

library(dplyr)

ESS %>% bind_rows(.id = 'id') %>% 
    group_by(id = as.integer(id)) %>% 
    summarise_all(sum)

#> # A tibble: 100 × 4
#>       id         v1         v2        v3
#>    <int>      <dbl>      <dbl>     <dbl>
#> 1      1 -15.829573 -24.786443  68.14273
#> 2      2   2.485961   4.103752  19.72813
#> 3      3  -2.034840  42.062543 -48.86363
#> 4      4  59.466600 -16.502055 -28.53470
#> 5      5 -30.416827  52.184059  14.68027
#> 6      6  34.931979   3.032825 -27.83873
#> 7      7 -23.792706 -23.910489  30.40869
#> 8      8  12.502404 -39.167088   8.91531
#> 9      9   6.110009  20.591199 -26.22461
#> 10    10 -29.776806  97.961942 -11.75786
#> # ... with 90 more rows