[R] Aggregate: assembling the "by" part on the fly

Andrew C. Ward andreww at cheque.uq.edu.au
Tue Jul 23 19:11:35 CEST 2002

Dear R users,

I'm having trouble using aggregate() and would
greatly appreciate your advice. I am using R 1.5.1
on Windows 2000.

I want to call my function in the following way
	extract.data(x=dat[, "Age", "Year"])
where extract.data() uses aggregate() to count the number
of cases for each combination of "Age" and "Year".

I've defined extract.data() in the following way
	extract.data <- function(x) {
		tmp <- aggregate(x[,1], by=list(dimnames(x)[[2]]), length)
		dimnames(tmp)[[2]] <- c(dimnames(x)[[2]], "N")
The call to aggregate obviously won't work (and doesn't).

I'm wondering how to assemble the "by" argument inside
extract.data() when I wish aggregate() to use all the
columns in my data frame.

Thank you very much for your help.


Andrew C. Ward

CAPE Centre
Department of Chemical Engineering
The University of Queensland
Brisbane Qld 4072 Australia
andreww at cheque.uq.edu.au

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list