R Reshape Cast with Asymmetric Function -


how reshape data using cast asymmetric function? have data

>t    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 difference

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

if wanted sum, it'd easy: cast(t, ~ ., fun.aggregate=sum, value = 'c'). since difference asymmetric, don't know ensure b==1 value subtracted b==2 value , not vice versa.

thanks!

you can use diff function:

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

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -