Skip to content
Prev 316783 / 398506 Next

Script for conditional sums of vectors

Hello,

First, don't use "data" for a data frame, as it is a R function.

Here is a way to do what you are looking for:

a=c(1,1,1,1,1,1,2,2,2,2,2,2)
b=c(1,1,1,2,3,4,1,1,2,2,3,4)
c=c(400,200,300,100,500,300,200,100,500,400,200,100)

dat=data.frame(a=a,b=b,c=c)

dat.sum <- aggregate(c ~ a+b, dat, sum)
dat.sum <- reshape(dat.sum, timevar='b', idvar='a', direction='wide')

colnames(dat.sum) <- c('a','b.1','b.2','b.3','b.4')

HTH,
Pascal


Le 04/02/2013 18:29, Benjamin Gillespie a ?crit :