我有一个大清单,我想在此列表的每个对象中执行每行的rowSums
。
这就是我所拥有的:
> DFTYPE
[[1]]
[[1]][[1]]
DGDG.36.5 DGDG.36.6
1 978.4906 3585.613
1 1280.9531 8740.503
1 1228.3969 6082.900
1 5245.2437 18104.022
1 0.0000 827.650
[[1]][[2]]
DGDG.36.5 DGDG.36.6
2 0.0000 917.5844
2 513.5063 976.2344
2 516.5781 1540.6000
2 838.5406 3486.5563
2 1146.8812 3962.2906
2 0.0000 1050.5281
[[1]][[3]]
DGDG.36.5 DGDG.36.6
3 1755.0656 10924.975
3 713.2969 4681.759
3 946.1094 3319.722
3 1569.5281 6531.266
3 1827.9156 10501.375
3 0.0000 1973.325
[[2]]
[[2]][[1]]
MGDG.34.4 MGDG.34.5 MGDG.34.6 MGDG.36.4 MGDG.36.5 MGDG.36.6
1 891.950 1463.695 14266.34 0.000 1368.040 5015.425
1 2596.695 4228.470 38945.15 1531.350 5617.475 14647.965
1 1536.595 3273.335 30895.44 1610.060 4415.735 11106.930
1 4471.495 9894.595 71023.04 4086.105 9536.450 24968.695
[[2]][[2]]
MGDG.34.4 MGDG.34.5 MGDG.34.6 MGDG.36.4 MGDG.36.5 MGDG.36.6
2 0 0.0 2158.405 0.00 1478.710 2350.265
2 0 0.0 1409.740 0.00 1776.775 2110.440
2 0 0.0 1910.240 0.00 1044.570 2410.740
2 0 913.5 4558.720 1071.78 1499.770 3616.110
2 0 0.0 7980.890 1993.40 1044.855 8468.875
2 0 0.0 1593.260 792.46 0.000 1019.770
[[2]][[3]]
MGDG.34.4 MGDG.34.5 MGDG.34.6 MGDG.36.4 MGDG.36.5 MGDG.36.6
3 1225.095 1994.225 20028.34 1728.695 3503.870 11717.955
3 948.865 1284.680 9522.01 487.985 1702.520 4291.785
3 0.000 1396.415 10216.45 1062.240 1438.825 5163.490
3 1077.845 2422.845 20380.78 2904.935 3147.180 8760.835
3 1135.710 3821.430 29022.75 1555.960 3652.580 12318.555
3 0.000 870.055 12322.73 0.000 1652.525 5136.380
然后我创建一个新列表来获得总和的结果:
> DFttt<-list()
> DFttt<-rowSums(DFTYPE)
Error in rowSums(DFTYPE) :
'x' must be an array of at least two dimensions
答案 0 :(得分:4)
考虑一个名为List
的列表,如下所示:
set.seed(1)
List <- list(list(matrix(rnorm(10), ncol=2), matrix(rnorm(10), ncol=2)),
list(matrix(rnorm(10), ncol=2), matrix(rnorm(10), ncol=2)))
lapply(List, function(x) lapply(x, rowSums))
[[1]]
[[1]][[1]]
[1] -1.44692219 0.67107238 -0.09730391 2.17106215 0.02411938
[[1]][[2]]
[1] 1.4668476 0.3736530 0.3225956 -1.3934787 1.7188322
[[2]]
[[2]][[1]]
[1] 0.8628486 0.6263408 -1.3961874 -2.4675018 1.0377673
[[2]][[2]]
[1] 0.9436850 -0.4970777 0.3283582 1.0462203 -0.6138838
答案 1 :(得分:4)
我认为你需要一个双lapply
。
使用以下数据:
df <- data.frame(x=1:5,y=2:6)
df2 <- data.frame(u=3:7,v=4:8)
mylist <- list(list(df, df2),list(df,df2))
mylist
[[1]]
[[1]][[1]]
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
[[1]][[2]]
u v
1 3 4
2 4 5
3 5 6
4 6 7
5 7 8
[[2]]
[[2]][[1]]
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
[[2]][[2]]
u v
1 3 4
2 4 5
3 5 6
4 6 7
5 7 8
您可以使用:
lapply(mylist, function(l) lapply(l, rowSums))
给出了:
[[1]]
[[1]][[1]]
[1] 3 5 7 9 11
[[1]][[2]]
[1] 7 9 11 13 15
[[2]]
[[2]][[1]]
[1] 3 5 7 9 11
[[2]][[2]]
[1] 7 9 11 13 15
答案 2 :(得分:0)
您的示例不可重复,但类似于:
DFttt = lapply(DFTYPE, rowSums)
应该诀窍。