【发布时间】:2016-11-01 23:32:26
【问题描述】:
我想从数据框列表中生成一个数据框,其中所有列都相同,除了一列“收入”应该是列表中所有“收入”的总和。
这是我的数据框列表
mylist= structure(list(`1` = structure(list(ID = c(36L, 37L, 38L, 39L), Income = c(0, 0, 0, 9100)), .Names = c("ID", "Income"), row.names = c(1L, 2L, 3L, 4L), class = "data.frame"), `2` = structure(list(ID = c(36L, 37L, 38L, 39L), Income = c(0, 0, 0, 0)), .Names = c("ID", "Income"), row.names = c(1L, 2L, 3L, 4L), class = "data.frame"), `3` = structure(list(ID = c(36L, 37L, 38L, 39L), Income = c(7360, 0, 0, 0)), .Names = c("ID", "Income"), row.names = c(1L, 2L, 3L, 4L), class = "data.frame")))
> mylist
$`1`
ID Income
1 36 0
2 37 0
3 38 0
4 39 9100
$`2`
ID Income
1 36 0
2 37 0
3 38 0
4 39 0
$`3`
ID Income
1 36 7360
2 37 0
3 38 0
4 39 0
这就是我想做的:
ID Income
34 36 7360
26 37 0
23 38 0
15 39 9100
我曾尝试使用 reduce() 进行求和,但它创建了一个我想避免的单独列:
Reduce(function(df1, df2) data.frame(df1[,], res=df1["Income"] + df2["Income"]),mylist)
【问题讨论】: