Skip to content
Prev 275022 / 398506 Next

Calculating differences

Hi:

Here's one way with the plyr package. Using ds as the name of your
data frame (thank you for the dput and clear description of what you
wanted, BTW),

library('plyr')
ddply(ds, .(date), mutate, minspd = min(speed), Cmin =
C[which.min(speed)], diff = C - Cmin)
  speed     C house date hour    id minspd  Cmin   diff
1     3 0.697     1  719   18  1000      3 0.697  0.000
2     9 0.011     1 1027    8 10000      4 0.015 -0.004
3     4 0.015     1 1027    8 10001      4 0.015  0.000
4     8 0.012     1 1027    8 10002      4 0.015 -0.003
5     7 0.018     1 1030   11 10003      6 0.019 -0.001
6     6 0.019     1 1030   11 10004      6 0.019  0.000

HTH,
Dennis
On Thu, Oct 20, 2011 at 2:56 PM, Jeffrey Joh <johjeffrey at hotmail.com> wrote: