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!