R编程——具有公共值的行的和元素

时间:2022-09-23 13:09:20

Hello and thank you in advance for your assistance,

您好,感谢您的帮助,

(PLEASE Note Comments section for additional insight: i.e. the cost column in the example below was added to this question; Simon, provides a great answer, but the cost column itself is not represented in the data response from him, although the function he provides works with the cost column)

(请注意评论一节以获得更多的了解:即下面示例中的成本列被添加到这个问题中;Simon给出了一个很好的答案,但是cost列本身并不表示在他的数据响应中,尽管他提供的函数与cost列是一致的)

I have a data set, lets call it 'data' which looks like this

我有一个数据集,我们称它为,data,它是这样的

NAME     DATE     COLOR   PAID    COST
Jim      1/1/2013 GREEN   150     100
Jim      1/2/2013 GREEN   50      25
Joe      1/1/2013 GREEN   200     150
Joe      1/2/2013 GREEN   25      10

What I would like to do is sum the PAID (and COST) elements of the records with the same NAME value and reduce the number of rows (as in this example) to 2, such that my new data frame looks like this:

我想做的是用相同的名称值将记录的已支付(和成本)元素相加,并将行数(如本例中所示)减少到2,这样我的新数据框就会如下所示:

NAME     DATE     COLOR   PAID    COST
Jim      1/2/2013 GREEN   200     125
Joe      1/2/2013 GREEN   225     160

As far as the dates are concerned, I don't really care about which one survives the summation process.

就日期而言,我并不关心哪个能在求和过程中幸存下来。

I've gotten as far as rowSums(data), but I'm not exactly certain how to use it. Any help would be greatly appreciated.

我已经得到了行和(数据),但我不确定如何使用它。如有任何帮助,我们将不胜感激。

1 个解决方案

#1


21  

aggregate is the function you are looking for:

聚合是您要查找的函数:

aggregate( cbind( PAID , COST ) ~ NAME + COLOR , data = data , FUN = sum )
# NAME PAID
# 1  Jim  200
# 2  Joe  225

#1


21  

aggregate is the function you are looking for:

聚合是您要查找的函数:

aggregate( cbind( PAID , COST ) ~ NAME + COLOR , data = data , FUN = sum )
# NAME PAID
# 1  Jim  200
# 2  Joe  225