2
我有这样的例子data.frame
Aggreageting在data.frame行:通过分配列
set.seed(1)
df <- data.frame(id = letters[1:10], a = sample(100,10), b = sample(100,10),
aggregate_with = c(rep(NA,6),"y","b","b","e"), aggregate_order = c(rep(NA,6),"a,b","a,b","b,a","a,b"))
> df
id a b aggregate_with aggregate_order
1 a 27 21 <NA> <NA>
2 b 37 18 <NA> <NA>
3 c 57 68 <NA> <NA>
4 d 89 38 <NA> <NA>
5 e 20 74 <NA> <NA>
6 f 86 48 <NA> <NA>
7 g 97 98 y a,b
8 h 62 93 b a,b
9 i 58 35 b b,a
10 j 6 71 e a,b
我想,他们的aggregate_with
值匹配其他行的id
值(有效集合行的行自身aggregate_with
值无法匹配它自己的id
值),我想要应用的函数是根据aggregate_order
列中的分配将它们的a
和b
的值相加。聚合行的id
,aggregate_with
和aggregate_order
应保留由aggregate_with
列指示的行的值。
下面是导致data.frame
应该是什么样子:
> aggregated.df
id a b aggregate_with aggregate_order
1 a 27 21 <NA> <NA>
2 b 134 169 <NA> <NA>
3 c 57 68 <NA> <NA>
4 d 89 38 <NA> <NA>
5 e 26 145 <NA> <NA>
6 f 86 48 <NA> <NA>
7 g 97 98 y a,b
正如你所看到的,列在aggregated.df
第2行的a
是a
列a
,的总和,和行2,8 b
,一9分别在df
,反之亦然b
列。列a
和b
第aggregated.df
行第5行的a
和b
行df
第5行和第10行。尽管df
中的第7行的值为aggregate_with
,但它不存在于df
中,因此未汇总。
循环 - 但认为有一个更优雅的解决方案。 – user1701545
你应该用自己所拥有的东西进行编辑,这样人们就不会花费很多精力去到你已经存在的地方。 – alistaire