Skip to content

Calculating difference between values in data frame based on separate column

3 messages · Nathan Miller, Sarah Goslee, Dennis Murphy

#
Hi,

It shouldn't be so complicated. What about simply:
vial 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
[1] 14 15  5 28
vial 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:

  
    
#
Here's another way, using the reshape2 package:

library(reshape2)
d <- dcast(df, vial ~ measure, value_var = 'value')
d$diff <- with(d, B - A)
vial  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: