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

Posted

技术标签:

【中文标题】如何聚合列表中的所有数据?【英文标题】:How to aggregate all data in a list? 【发布时间】:2020-07-20 17:45:05 【问题描述】:

这是我的示例数据;

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))

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

我应该怎么做才能在一个函数中聚合所有列表元素?

【问题讨论】:

【参考方案1】:

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

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

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 
#...
#...

【讨论】:

【参考方案2】:

使用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 

【讨论】:

以上是关于如何聚合列表中的所有数据?的主要内容,如果未能解决你的问题,请参考以下文章

从数据库系统表中选择所有索引,根据Oracle中的索引聚合数据

Linq 接口列表,接口有成员字符串数组,我想要 linq 语句聚合对象数组中的所有字符串

如何修复 BigQuery 中的错误“选择列表表达式 [...] 既不分组也不聚合的引用”?

MySQL 中的聚合函数 - 列表(如 Oracle 中的 LISTAGG)

SQL Server中的聚合函数都有哪些?

使用 mongoDB 中的聚合删除集合中所有文档的特定字段