Trying to use dates in their R-native form (e.g., POSIXct) rather than turning them into character strings, I've encountered the following problem. I create a data frame where one column is dates. Then I use "by()" to do a calculation on grouped subsets of the data. When I try to extract values from the result, I get "subscript out of bounds". The example below shows the error.> x <- data.frame(A= c("X", "Y", "X", "X", "Y", "Y", "Z" ), D as.POSIXct(c("2008-11-03","2008-11-03","2008-11-03", "2008-11-04","2009-01-13", "2009-01-13", "2009-01-13")), Z = 1:7)> m <- by(x, list(A=x$A, D=x$D), function(v) { sum(v$Z); }) > m[x$A[1], x$D[1]]Error: subscript out of bounds Rgds, Oren Cheyette [[alternative HTML version deleted]]
Dear Oren, Try this:> x <- data.frame(A= c("X", "Y", "X", "X", "Y", "Y", "Z" ), D + as.POSIXct(c("2008-11-03","2008-11-03","2008-11-03", "2008-11-04",+ "2009-01-13", "2009-01-13", "2009-01-13")), Z = 1:7)> > m<-with(x,tapply(Z, list(A,D), sum)) > m[rownames(m)==x$A[1],]2008-11-03 2008-11-04 2009-01-13 4 4 NA> m[,colnames(m)==x$D[1]]X Y Z 4 2 NA> m[rownames(m)==x$A[1],colnames(m)==x$D[1]][1] 4> x$A[1][1] X Levels: X Y Z> x$D[1][1] "2008-11-03 EST" See ?with, ?tapply, ?rownames, and ?colnames for more information. HTH, Jorge On Wed, Mar 4, 2009 at 6:22 PM, oren cheyette <ocheyette@gmail.com> wrote:> Trying to use dates in their R-native form (e.g., POSIXct) rather than > turning them into character strings, I've encountered the following > problem. > I create a data frame where one column is dates. Then I use "by()" to do a > calculation on grouped subsets of the data. When I try to extract values > from the result, I get "subscript out of bounds". The example below shows > the error. > > > x <- data.frame(A= c("X", "Y", "X", "X", "Y", "Y", "Z" ), D > as.POSIXct(c("2008-11-03","2008-11-03","2008-11-03", "2008-11-04", > "2009-01-13", "2009-01-13", "2009-01-13")), Z = 1:7) > > m <- by(x, list(A=x$A, D=x$D), function(v) { sum(v$Z); }) > > m[x$A[1], x$D[1]] > Error: subscript out of bounds > > > Rgds, > Oren Cheyette > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Wed, Mar 4, 2009 at 5:22 PM, oren cheyette <ocheyette at gmail.com> wrote:> Trying to use dates in their R-native form (e.g., POSIXct) rather than > turning them into character strings, I've encountered the following problem. > I create a data frame where one column is dates. Then I use "by()" to do a > calculation on grouped subsets of the data. When I try to extract values > from the result, I get "subscript out of bounds". The example below shows > the error. > >> x <- data.frame(A= c("X", "Y", "X", "X", "Y", "Y", "Z" ), D > as.POSIXct(c("2008-11-03","2008-11-03","2008-11-03", "2008-11-04", > "2009-01-13", "2009-01-13", "2009-01-13")), Z = 1:7) >> m <- by(x, list(A=x$A, D=x$D), function(v) { sum(v$Z); }) >> m[x$A[1], x$D[1]] > Error: subscript out of bounds > > > Rgds, > Oren CheyetteYou might also want to take a look at the plyr package: install.packages("plyr") library(plyr) ddply(x, .(A, D), function(df) sum(df$Z)) dlply(x, .(A, D), function(df) sum(df$Z)) More info at http://had.co.nz/plyr Hadley -- http://had.co.nz/
hadley wickham <h.wickham <at> gmail.com> writes:> > You might also want to take a look at the plyr package: > > install.packages("plyr") > library(plyr) > ddply(x, .(A, D), function(df) sum(df$Z)) > dlply(x, .(A, D), function(df) sum(df$Z)) >I recently for the first time used the plyr package in an introductory course as a general replacement of for "by", "(x)apply", followed by do.call gymnastics. It was an amazing step forward to have a nicely orthogonal set of function that returns what people expected instead of converting everything to a list. I know, real MEN program assembler and love (x)apply... Dieter