How to sum one variable group by two variables?? R language -


this question has answer here:

i have 1 dataframe:

    date   area    sales  1     201204   shanghai    23  2     201204 beijing     25  3     201204 beijing     16  4     201205 shanghai    55  5     201205 beijing     17  6     201205 shanghai    16 

what want output table follows:

date   shanghai  beijing   201204  23        41  201205  71        17 

how in r? thanks!

two options, 1 base function aggregate, other plyr's ddply. made own data since yours isn't reproducible, should able point:

dat <- data.frame(x1 = sample(letters[1:3], 100, true),                   x2 = sample(letters[6:3], 100, true),                   value = rnorm(100))  aggregate(value ~ x1 + x2, data = dat, fun = 'sum')  library(plyr) ddply(dat, .(x1,x2), summarize, foo = sum(value)) 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -