rreshapeasymmetric

R Reshape Cast with Asymmetric Function


How do you reshape data using cast with an asymmetric function? I have the data

>t
   a b  c
1  1 1 30
2  1 2 25
3  2 1 59
4  2 2  1
5  3 1 12
6  3 2 97
7  4 1 66
8  4 2 43
9  5 1 13
10 5 2 32

For each level x of a I'd like to get the difference

t[t$a==x & t$b==2, 'c'] - t[t$a==x & t$b==1, 'c']

If I wanted a sum, it'd be easy: cast(t, a ~ ., fun.aggregate=sum, value = 'c'). But since difference is asymmetric, I don't know to ensure that the b==1 value would be subtracted from b==2 value and not vice versa.

Thanks!


Solution

  • You can use the diff function:

    library(reshape)
    t2 <- t[order(t$b), ] # to make sure '1' comes before '2'
    cast(t2, a ~ ., fun.aggregate = diff, value = 'c')
    
      a (all)
    1 1    -5
    2 2   -58
    3 3    85
    4 4   -23
    5 5    19