fork download
  1. library(magrittr)
  2. d <-
  3. data.frame(
  4. FW = gl(3, 3, 18),
  5. Difficulty = gl(3, 1, 18, labels = c("L", "M", "H")),
  6. E = rnorm(9)
  7. )
  8. d
  9.  
  10. # 用 reshape2 package 的 dcast
  11. library(reshape2)
  12. d %>%
  13. reshape2::dcast(Difficulty ~ FW, fun.aggregate = mean, value.var = "E")
  14.  
  15. # 用 data.table package 的 dcast
  16. library(data.table)
  17. d %>%
  18. as.data.table %>%
  19. data.table::dcast(Difficulty ~ FW, fun.aggregate = mean, value.var = "E")
  20.  
  21.  
Success #stdin #stdout #stderr 0.51s 53788KB
stdin
Standard input is empty
stdout
   FW Difficulty           E
1   1          L -0.35443827
2   1          M -0.31200287
3   1          H  0.07852952
4   2          L -0.08490950
5   2          M -1.02687901
6   2          H  1.96538016
7   3          L -0.79581132
8   3          M -0.77655673
9   3          H -0.41827920
10  1          L -0.35443827
11  1          M -0.31200287
12  1          H  0.07852952
13  2          L -0.08490950
14  2          M -1.02687901
15  2          H  1.96538016
16  3          L -0.79581132
17  3          M -0.77655673
18  3          H -0.41827920
  Difficulty           1          2          3
1          L -0.35443827 -0.0849095 -0.7958113
2          M -0.31200287 -1.0268790 -0.7765567
3          H  0.07852952  1.9653802 -0.4182792
   Difficulty           1          2          3
1:          L -0.35443827 -0.0849095 -0.7958113
2:          M -0.31200287 -1.0268790 -0.7765567
3:          H  0.07852952  1.9653802 -0.4182792
stderr
Attaching package: ‘data.table’

The following objects are masked from ‘package:reshape2’:

    dcast, melt