[R] Sum of specific column

Stephen Weigand weigand.stephen at gmail.com
Thu Apr 26 04:10:13 CEST 2007

On 4/25/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> I have a data set that I have imported (not sure if that makes a
> difference) and I would like to calculate the sum of only specific
> columns.  I have tried
> >colSums(dataset, by=list(dataset$col5), dims=1) and I get an error of
> unused arguments

The error message is helpful: there is no 'by' argument to colSums.
You'll just get column sums over all rows.

> I have also tried
> >aggregate(dataset, by=list(dataset$col5), sum) and I get the error that
> sum is not meaningful for factors.

Instead of giving aggregate the whole dataset, you can specify certain
columns via dataset[, c(1,5)] or dataset[, c("height", "weight")].

> I want to only calculate the sum for specific columns because some of
> the columns have words in them and I have not been able to find anything
> else that would help or why these errors are occuring.
> Jacquie

Good luck,


Rochester, Minn. USA

More information about the R-help mailing list