如何聚合列表中的所有数据?

How to aggregate all data in a list?

这是我的示例数据;

z1<-list(`Cluster 1` = structure(list(Day_1 = structure(c(Hour_1 = 0, 
Hour_2 = 0, Hour_3 = 0, Hour_4 = 0, Hour_5 = 0, Hour_6 = 0, Hour_7 = 0, 
Hour_8 = 0, Hour_9 = 0, Hour_10 = 0, Hour_11 = 0, Hour_12 = 0, 
Hour_13 = 0, Hour_14 = 0, Hour_15 = 0, Hour_16 = 0, Hour_17 = 0, 
Hour_18 = 0.041, Hour_19 = 0.673, Hour_20 = 0, Hour_21 = 0, Hour_22 = 0, 
Hour_23 = 0.319, Hour_24 = 0.447), .Dim = 24L, .Dimnames = list(
    c("Hour_1", "Hour_2", "Hour_3", "Hour_4", "Hour_5", "Hour_6", 
    "Hour_7", "Hour_8", "Hour_9", "Hour_10", "Hour_11", "Hour_12", 
    "Hour_13", "Hour_14", "Hour_15", "Hour_16", "Hour_17", "Hour_18", 
    "Hour_19", "Hour_20", "Hour_21", "Hour_22", "Hour_23", "Hour_24"
    ))), Day_2 = structure(c(Hour_1 = 1.07, Hour_2 = 0, Hour_3 = 0, 
Hour_4 = 0, Hour_5 = 0, Hour_6 = 1.27, Hour_7 = 0.19, Hour_8 = 0, 
Hour_9 = 0, Hour_10 = 0, Hour_11 = 0, Hour_12 = 0, Hour_13 = 0, 
Hour_14 = 0, Hour_15 = 0, Hour_16 = 0, Hour_17 = 0, Hour_18 = 0, 
Hour_19 = 0, Hour_20 = 0, Hour_21 = 0, Hour_22 = 0, Hour_23 = 0, 
Hour_24 = 0), .Dim = 24L, .Dimnames = list(c("Hour_1", "Hour_2", 
"Hour_3", "Hour_4", "Hour_5", "Hour_6", "Hour_7", "Hour_8", "Hour_9", 
"Hour_10", "Hour_11", "Hour_12", "Hour_13", "Hour_14", "Hour_15", 
"Hour_16", "Hour_17", "Hour_18", "Hour_19", "Hour_20", "Hour_21", 
"Hour_22", "Hour_23", "Hour_24")))), .Dim = 2L, .Dimnames = list(
    c("Day_1", "Day_2"))), `Cluster 2` = structure(list(Day_3 = structure(c(Hour_1 = 0, 
Hour_2 = 0, Hour_3 = 0, Hour_4 = 0, Hour_5 = 0, Hour_6 = 0, Hour_7 = 0, 
Hour_8 = 0, Hour_9 = 0, Hour_10 = 0, Hour_11 = 0, Hour_12 = 0, 
Hour_13 = 0, Hour_14 = 0, Hour_15 = 0, Hour_16 = 0, Hour_17 = 0.189, 
Hour_18 = 0.001, Hour_19 = 0, Hour_20 = 0, Hour_21 = 0, Hour_22 = 0, 
Hour_23 = 0, Hour_24 = 0), .Dim = 24L, .Dimnames = list(c("Hour_1", 
"Hour_2", "Hour_3", "Hour_4", "Hour_5", "Hour_6", "Hour_7", "Hour_8", 
"Hour_9", "Hour_10", "Hour_11", "Hour_12", "Hour_13", "Hour_14", 
"Hour_15", "Hour_16", "Hour_17", "Hour_18", "Hour_19", "Hour_20", 
"Hour_21", "Hour_22", "Hour_23", "Hour_24"))), Day_4 = structure(c(Hour_1 = 0, 
Hour_2 = 0.521, Hour_3 = 0.229, Hour_4 = 0, Hour_5 = 0, Hour_6 = 0, 
Hour_7 = 0, Hour_8 = 0, Hour_9 = 0, Hour_10 = 0, Hour_11 = 0, 
Hour_12 = 0, Hour_13 = 0, Hour_14 = 0, Hour_15 = 0, Hour_16 = 0, 
Hour_17 = 0, Hour_18 = 0, Hour_19 = 0, Hour_20 = 0, Hour_21 = 0, 
Hour_22 = 0, Hour_23 = 0, Hour_24 = 0), .Dim = 24L, .Dimnames = list(
    c("Hour_1", "Hour_2", "Hour_3", "Hour_4", "Hour_5", "Hour_6", 
    "Hour_7", "Hour_8", "Hour_9", "Hour_10", "Hour_11", "Hour_12", 
    "Hour_13", "Hour_14", "Hour_15", "Hour_16", "Hour_17", "Hour_18", 
    "Hour_19", "Hour_20", "Hour_21", "Hour_22", "Hour_23", "Hour_24"
    )))), .Dim = 2L, .Dimnames = list(c("Day_3", "Day_4"))))

例如,我可以使用下面的代码聚合第一个列表元素 (z1$`Cluster 1`$Day_1)

agg<-lapply(c(2, 3, 4),  function(xx) tapply(z1$`Cluster 1`$Day_1, as.integer(gl(24, xx, 24) ), FUN = sum))

但是当我尝试将此函数应用于所有列表元素时出现错误。

如何将所有列表元素聚合到一个函数中?

您可能应该考虑重组数据,因为嵌套列表可能会变得过于复杂。

这是对当前表单中的数据执行此操作的一种方法。

lapply(z1, function(x) lapply(x, function(y)
        lapply(c(2, 3, 4), function(xx) 
              tapply(y, as.integer(gl(24, xx, 24)), FUN = sum))))


#$`Cluster 1`
#$`Cluster 1`$Day_1
#$`Cluster 1`$Day_1[[1]]
#    1     2     3     4     5     6     7     8     9    10    11    12 
#0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.041 0.673 0.000 0.766 

#$`Cluster 1`$Day_1[[2]]
#    1     2     3     4     5     6     7     8 
#0.000 0.000 0.000 0.000 0.000 0.041 0.673 0.766 

#$`Cluster 1`$Day_1[[3]]
#    1     2     3     4     5     6 
#0.000 0.000 0.000 0.000 0.714 0.766 
#...
#...

使用 rapply.

res <- rapply(z1, function(x) 
  lapply(2:4, function(xx) tapply(x, as.integer(gl(24, xx, 24)), sum)), how="list")

res[[1]]
# $Day_1
# $Day_1[[1]]
#     1     2     3     4     5     6     7     8     9    10    11    12 
# 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.041 0.673 0.000 0.766 
# 
# $Day_1[[2]]
#     1     2     3     4     5     6     7     8 
# 0.000 0.000 0.000 0.000 0.000 0.041 0.673 0.766 
# 
# $Day_1[[3]]
#     1     2     3     4     5     6 
# 0.000 0.000 0.000 0.000 0.714 0.766 
# 
# 
# $Day_2
# $Day_2[[1]]
#    1    2    3    4    5    6    7    8    9   10   11   12 
# 1.07 0.00 1.27 0.19 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
# 
# $Day_2[[2]]
#    1    2    3    4    5    6    7    8 
# 1.07 1.27 0.19 0.00 0.00 0.00 0.00 0.00 
# 
# $Day_2[[3]]
#    1    2    3    4    5    6 
# 1.07 1.46 0.00 0.00 0.00 0.00