根据匹配列减去列对 [英] Subtract pairs of columns based on matching column
问题描述
我会提前道歉-我知道其他地方可能已经回答了这个问题,但是我似乎无法找到所需的答案,也无法设法将找到的其他代码适应我的需要
I'll apologise in advance - I know this has likely been answered elsewhere, but I don't seem to be able to find the answer I need, and can't manage to adapt other code I have found to my needs.
我有一个数据框:
FILE | TECHNIQUE | COUNT
------------------------
A | ONE | 10
A | TWO | 25
B | ONE | 5
B | TWO | 30
C | ONE | 30
C | TWO | 50
我想产生一个COUNT和COUNT之间的差值的数据帧,每个文件都有一行,即
I would like to produce a data frame of the difference of the COUNT values between ONE and TWO, with a row for each FILE, i.e.
FILE | DIFFERENCE
-----------------
A | 15
B | 25
C | 20
我相信我可以使用base R或Plyr相当容易地做到这一点,但是有点卡住了.谁能建议一个好的方法,也许还有关于Plyr的好的教程,将来可能会帮助我解决类似的问题?
I'm convinced I should be able to do this fairly easily with base R or Plyr, but am a bit stuck. Could anyone suggest a good way to do this, and perhaps good tutorials on Plyr that might help me with similar problems in the future?
谢谢
推荐答案
在基础中使用aggregate
:
> aggregate(.~FILE, data= DF[, -2], FUN=diff)
FILE COUNT
1 A 15
2 B 25
3 C 20
在plyr中使用ddply
> ddply(DF[,-2], .(FILE), summarize, DIFFERENCE=diff(COUNT))
FILE DIFFERENCE
1 A 15
2 B 25
3 C 20
与data.table
> # library(data.table)
> DT <- data.table(DF)
> DT[, diff(COUNT), by=FILE]
FILE V1
1: A 15
2: B 25
3: C 20
与by
> with(DF, by(COUNT, FILE, diff))
FILE: A
[1] 15
-----------------------------------------------------------------------------
FILE: B
[1] 25
-----------------------------------------------------------------------------
FILE: C
[1] 20
与tapply
> tapply(DF$COUNT, DF$FILE, diff)
A B C
15 25 20
从doBy包中
与summaryBy
一起
with summaryBy
from doBy package
> # library(doBy)
> summaryBy(COUNT~FILE, FUN=diff, data=DF)
FILE COUNT.diff
1 A 15
2 B 25
3 C 20
更新 占百分比:
> aggregate(.~FILE, data= DF[, -2], function(x) (x[1]/x[2])*100)
FILE COUNT
1 A 40.00000
2 B 16.66667
3 C 60.00000
这篇关于根据匹配列减去列对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!