Reputation: 3201
I'm aggregating all of the variables in the TEF data frame by the variable TEF using the following code :
TEF2<-aggregate(cbind(column2, column3)~TEF, data=TEF, sum, na.rm=TRUE)
This works great for the variables in columns 2 and 3. However the trouble is I have 338 variables to aggregate and don't want to type out all their names.
I've tried using
TEF2<-aggregate(cbind(2:339)~TEF, data=TEF, sum, na.rm=TRUE)
but this doesn't work, does anyone have any suggestions?
Thanks
Upvotes: 2
Views: 3089
Reputation: 132969
Use the .
notation in the formula. That takes all columns except the grouping column:
TEF <- data.frame(matrix(rnorm(100),nrow=10),TEF=1:2)
TEF2 <- aggregate(.~TEF, data=TEF, sum, na.rm=TRUE)
Upvotes: 2