I have been struggling to find the best way to do this subtraction within groups. I have data frame containing a long list of samples (Sample
), each one treate
Try this
> transform(df, dValue=ave(value, Sample, FUN=function(x) c(0, x[1]-x[-1])))
Sample condition value dValue
1 var1 A 12 0
2 var1 B 14 -2
3 var1 C 15 -3
4 var2 A 20 0
5 var2 B 19 1
6 var2 C 19 1
7 var3 A 50 0
8 var3 B 51 -1
9 var3 C 48 2
I'm assuming df
looks like this
Sample condition value
1 var1 A 12
2 var1 B 14
3 var1 C 15
4 var2 A 20
5 var2 B 19
6 var2 C 19
7 var3 A 50
8 var3 B 51
9 var3 C 48
You can also do that with dplyr
:
require(dplyr)
df %.%
group_by(Sample) %.%
mutate(dValue = value[condition == "A"] - value)
# Sample condition value dValue
#1 var1 A 12 0
#2 var1 B 14 -2
#3 var1 C 15 -3
#4 var2 A 20 0
#5 var2 B 19 1
#6 var2 C 19 1
#7 var3 A 50 0
#8 var3 B 51 -1
#9 var3 C 48 2
Assuming your dataset is called dat
, here's a couple of data.table
solutions:
require(data.table) ## >= 1.9.2
setDT(dat)[, dValue := value[condition == "A"] - value, by=Sample]
require(data.table) ## >= 1.9.2
setkey(setDT(dat), Sample)
dat[dat[condition == "A"], dValue := i.value-value]
# Sample condition value dValue
# 1: var1 A 12 0
# 2: var1 B 14 -2
# 3: var1 C 15 -3
# 4: var2 A 20 0
# 5: var2 B 19 1
# 6: var2 C 19 1
# 7: var3 A 50 0
# 8: var3 B 51 -1
# 9: var3 C 48 2