Nathan Miller
2011-Oct-21 22:31 UTC
[R] Calculating difference between values in data frame based on separate column
Hi all, Say I have a data frame something like the one below with different sample vials, measured before(B) and after(A) some process, with a value recorded at each measurement point vial measure value 1 B 26 1 A 12 2 B 45 2 A 30 3 B 32 3 A 27 4 B 34 4 A 6 Is there an easy means by which I can subtract the after (A) measurements from the before (B) measurement for each vial in this data frame so I am left with a data frame that contains the vial number and difference between A and B? I've played around with writing a function and applying it with ddply, but haven't stumbled on a technique that works, though I feel there should be something simple means of doing this that I'm just not seeing. Thanks for you help, Nate [[alternative HTML version deleted]]
Sarah Goslee
2011-Oct-21 22:44 UTC
[R] Calculating difference between values in data frame based on separate column
Hi, It shouldn't be so complicated. What about simply:> tdvial measure value 2 1 A 12 1 1 B 26 4 2 A 30 3 2 B 45 6 3 A 27 5 3 B 32 8 4 A 6 7 4 B 34> td <- td[order(td$vial, td$measure),] # make sure the samples are in order > # how to get the differences > td[td$measure == "B", "value"] - td[td$measure == "A", "value"][1] 14 15 5 28> > td.diff <- data.frame(vial = td[td$measure == "A", "vial"], diff = td[td$measure == "B", "value"] - td[td$measure == "A", "value"]) > td.diffvial diff 1 1 14 2 2 15 3 3 5 4 4 28>Sarah On Fri, Oct 21, 2011 at 6:31 PM, Nathan Miller <natemiller77 at gmail.com> wrote:> Hi all, > > Say I have a data frame something like the one below with different sample > vials, measured before(B) and after(A) some process, with a value recorded > at each measurement point > > vial ? ?measure ? ?value > 1 ? ? ? B ? ? ? ? ? ? ? ?26 > 1 ? ? ? A ? ? ? ? ? ? ? ?12 > 2 ? ? ? B ? ? ? ? ? ? ? ? 45 > 2 ? ? ? A ? ? ? ? ? ? ? ? 30 > 3 ? ? ? B ? ? ? ? ? ? ? ? 32 > 3 ? ? ? A ? ? ? ? ? ? ? ? 27 > 4 ? ? ? B ? ? ? ? ? ? ? ? 34 > 4 ? ? ? A ? ? ? ? ? ? ? ? 6 > > Is there an easy means by which I can subtract the after (A) measurements > from the before (B) measurement for each vial in this data frame so I am > left with a data frame that contains the vial number and difference between > A and B? I've played around with writing a function and applying it with > ddply, but haven't stumbled on a technique that works, though I feel there > should be something simple means of doing this that I'm just not seeing. > > Thanks for you help, > Nate >-- Sarah Goslee http://www.functionaldiversity.org
Dennis Murphy
2011-Oct-22 05:46 UTC
[R] Calculating difference between values in data frame based on separate column
Here's another way, using the reshape2 package: library(reshape2) d <- dcast(df, vial ~ measure, value_var = 'value') d$diff <- with(d, B - A)> dvial A B diff 1 1 12 26 14 2 2 30 45 15 3 3 27 32 5 4 4 6 34 28 HTH, Dennis On Fri, Oct 21, 2011 at 3:31 PM, Nathan Miller <natemiller77 at gmail.com> wrote:> Hi all, > > Say I have a data frame something like the one below with different sample > vials, measured before(B) and after(A) some process, with a value recorded > at each measurement point > > vial ? ?measure ? ?value > 1 ? ? ? B ? ? ? ? ? ? ? ?26 > 1 ? ? ? A ? ? ? ? ? ? ? ?12 > 2 ? ? ? B ? ? ? ? ? ? ? ? 45 > 2 ? ? ? A ? ? ? ? ? ? ? ? 30 > 3 ? ? ? B ? ? ? ? ? ? ? ? 32 > 3 ? ? ? A ? ? ? ? ? ? ? ? 27 > 4 ? ? ? B ? ? ? ? ? ? ? ? 34 > 4 ? ? ? A ? ? ? ? ? ? ? ? 6 > > Is there an easy means by which I can subtract the after (A) measurements > from the before (B) measurement for each vial in this data frame so I am > left with a data frame that contains the vial number and difference between > A and B? I've played around with writing a function and applying it with > ddply, but haven't stumbled on a technique that works, though I feel there > should be something simple means of doing this that I'm just not seeing. > > Thanks for you help, > Nate > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Seemingly Similar Threads
- Applying function with separate dataframe (calibration file) supplying some inputs
- Linear Regression with 2 grouping variables
- (Lattice) How to improve the readability of a bwplot, i.e. separating groups somehow
- indexing within the function "aggregate"
- assessing the fit of a LME model