[R] Aggregate: assembling the "by" part on the fly
Petr Pikal
petr.pikal at precheza.cz
Tue Jul 23 11:11:08 CEST 2002
On 23 Jul 2002 at 17:11, Andrew C. Ward wrote:
> 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")
> invisible(tmp)
> }
> The call to aggregate obviously won't work (and doesn't).
Well, I think you are not doing what you exactly want (and what aggregate
requires).
if your data frame looks for instance:
> daf
ID Age Year
1 1 5 1995
2 2 6 1995
3 3 8 1995
4 4 5 1995
5 5 6 1996
6 6 7 1996
7 7 5 1996
8 8 6 1996
9 9 7 1996
10 10 5 1996
aggregate will do what you want smoothly
> aggregate(daf[,1],list(daf$Age,daf$Year),length)
Group.1 Group.2 x
1 5 1995 2
2 6 1995 1
3 8 1995 1
4 5 1996 2
5 6 1996 2
6 7 1996 2
look at the difference in output
> list(daf$Age,daf$Year)
[[1]]
[1] 5 6 8 5 6 7 5 6 7 5
[[2]]
[1] 1995 1995 1995 1995 1996 1996 1996 1996 1996 1996
> list(dimnames(daf)[[2]])
[[1]]
[1] "ID" "Age" "Year"
>
> 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.
Using all columns seems to me a liitle bit surprising. Do you mean to use also a
column on which the aggregation is performed?
>
> Thank you very much for your help.
>
>
> Regards,
>
> Andrew C. Ward
>
> CAPE Centre
> Department of Chemical Engineering
> The University of Queensland
> Brisbane Qld 4072 Australia
> andreww at cheque.uq.edu.au
Best regardsPetr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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