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:> dafID 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.auBest 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I am trying the function tkscript, that apears in "A primer on the R-Tcl/Tk Package" on The Newsletter of the R Project of September 2001. But I am a newbee and I don?t know how to handle some problems. 1. When I "Load" a file, such a file appears without newline (All the text in a line) and only the first and the last line. 2. When I "Run" a ? symbol appears and I don't know what to do there. 3. If I type any letter and press return then this message appears: Executing from script window: ----- Error in cat(list(...), file, sep, fill, labels, append) : argument 3 not yet handled by cat The file I try to use with the tkscript function is attached too Thank you for your help Kenneth Cabrera -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: script.R Url: https://stat.ethz.ch/pipermail/r-help/attachments/20020723/80dc51c7/script.pl -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ejemplo.txt Url: https://stat.ethz.ch/pipermail/r-help/attachments/20020723/80dc51c7/ejemplo.txt
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). 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. Regards, 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thank you, Petr. Your example > aggregate(dat[,1],list(daf$Age,daf$Year),length) is exactly what I want, but without hard-coding the bits in list(...). In my case, aggregate is inside another function to which I pass a dataframe. The names of the columns will vary from call to call, as may the number of columns. Thanks again. Regards, Andrew C. Ward CAPE Centre Department of Chemical Engineering The University of Queensland Brisbane Qld 4072 Australia andreww at cheque.uq.edu.au On Tuesday, July 23, 2002 7:11 PM, Petr Pikal [SMTP:petr.pikal at precheza.cz] wrote:> > > 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._