Reputation: 131
I have a dataset (df) like this:
Iso conc. rep time OD
1 1 1 0 0.2
1 1.5 2 0 0.2
1 2 3 0 0.2
2 1 1 0 0.3
2 1.5 2 0 0.25
2 2 3 0 0.3
1 1 1 1 0.4
1 1.5 2 1 0.35
1 2 3 1 0.38
2 1 1 1 0.4
2 1.5 2 1 0.45
2 2 3 1 0.43
And I want to get the result growth=OD(time=1)-OD(time=0)
basing on Iso, conc, and rep.
The output would be like this:
Iso conc. rep time growth
1 1 1 1 0.2
1 1.5 2 1 0.15
1 2 3 1 0.18
2 1 1 1 0.1
2 1.5 2 1 0.2
2 2 3 1 0.13
I have been thinking to use data.table to calculate the growth.
DT <- as.data.table(df)
DT[, , by = .(Iso,conc.,rep,set)]
But I don't know how to write the part before two comma. Could somebody help me?
Upvotes: 1
Views: 53
Reputation: 78590
You can do this with:
DT [, list(growth = OD[time == 1] - OD[time == 0]), by=.(Iso,conc.,rep)]
Or alternatively, if you are sure there are only two values in each group:
DT [order(time), list(growth = diff(OD), by=.(Iso,conc.,rep)]
Upvotes: 1
Reputation: 24178
Using data.table
you can simply do:
dt[,.(growth = OD[time==1]-OD[time==0]),.(Iso,conc.,rep)]
# Iso conc. rep growth
#1: 1 1.0 1 0.20
#2: 1 1.5 2 0.15
#3: 1 2.0 3 0.18
#4: 2 1.0 1 0.10
#5: 2 1.5 2 0.20
#6: 2 2.0 3 0.13
Upvotes: 1