r - 如何计算“子”的总和'在嵌套列表中

时间:2018-01-31 21:16:07

标签: r nested-lists

我有一个嵌套列表mylist。在第一级&#39; level1有四种植被类型。在第二级&#39;中,我测量了15个土壤水平的水分胁迫(名为level2level15,依此类推,直至mylist <- structure(list(needleleaf_evergreen_temperate_tree = structure(list( layer1 = 0.074844339399475, layer2 = 0.10364235153792, layer3 = 0.139199261740378, layer4 = 0.16641474105245, layer5 = 0.169465314138987, layer6 = 0.144044051642624, layer7 = 0.105600767636715, layer8 = 0.0652887370818433, layer9 = 0.0264351101290891, layer10 = 0.00481604897579532, layer11 = 0.000247537572307209, layer12 = 1.73860803440901e-06, layer13 = 4.8438137437183e-10, layer14 = 6.6619638078275e-16, layer15 = 2.88818224958405e-25), .Names = c("layer1", "layer2", "layer3", "layer4", "layer5", "layer6", "layer7", "layer8", "layer9", "layer10", "layer11", "layer12", "layer13", "layer14", "layer15" )), needleleaf_evergreen_boreal_tree = structure(list(layer1 = 0.074844339399475, layer2 = 0.10364235153792, layer3 = 0.139199261740378, layer4 = 0.16641474105245, layer5 = 0.169465314138987, layer6 = 0.144044051642624, layer7 = 0.105600767636715, layer8 = 0.0652887370818433, layer9 = 0.0264351101290891, layer10 = 0.00481604897579532, layer11 = 0.000247537572307209, layer12 = 1.73860803440901e-06, layer13 = 4.8438137437183e-10, layer14 = 6.6619638078275e-16, layer15 = 2.88818224958405e-25), .Names = c("layer1", "layer2", "layer3", "layer4", "layer5", "layer6", "layer7", "layer8", "layer9", "layer10", "layer11", "layer12", "layer13", "layer14", "layer15")), needleleaf_deciduous_boreal_tree = structure(list( layer1 = 0.074844339399475, layer2 = 0.10364235153792, layer3 = 0.139199261740378, layer4 = 0.16641474105245, layer5 = 0.169465314138987, layer6 = 0.144044051642624, layer7 = 0.105600767636715, layer8 = 0.0652887370818433, layer9 = 0.0264351101290891, layer10 = 0.00481604897579532, layer11 = 0.000247537572307209, layer12 = 1.73860803440901e-06, layer13 = 4.8438137437183e-10, layer14 = 6.6619638078275e-16, layer15 = 2.88818224958405e-25), .Names = c("layer1", "layer2", "layer3", "layer4", "layer5", "layer6", "layer7", "layer8", "layer9", "layer10", "layer11", "layer12", "layer13", "layer14", "layer15" )), c3_irrigated = structure(list(layer1 = 0.0754105781789665, layer2 = 0.106401701696586, layer3 = 0.146857331360997, layer4 = 0.181972345543274, layer5 = 0.191196461077081, layer6 = 0.158267532821584, layer7 = 0.094842247909502, layer8 = 0.0370323148644247, layer9 = 0.00750914382571778, layer10 = 0.000504776733855199, layer11 = 5.5627446227042e-06, layer12 = 3.24337357358994e-09, layer13 = 1.50763789751944e-14, layer14 = 2.43174707104567e-23, layer15 = 1.55216144591354e-37), .Names = c("layer1", "layer2", "layer3", "layer4", "layer5", "layer6", "layer7", "layer8", "layer9", "layer10", "layer11", "layer12", "layer13", "layer14", "layer15"))), .Names = c("needleleaf_evergreen_temperate_tree", "needleleaf_evergreen_boreal_tree", "needleleaf_deciduous_boreal_tree", "c3_irrigated"))

这是我的数据集:

{{1}}

我需要做的是对每种植被类型的15个土壤水平上的水分压力求和(即总和所有&#39;以及列表中每个&#39;级别&#39;)。

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

只需致电lapply,由于嵌套内容位于列表中,请运行unlist将子列表项转换为sum的数字向量:

newlist <- lapply(mylist, function(x) sum(unlist(x)))