[R] Bug in by() with dates as levels?

hadley wickham h.wickham at gmail.com
Thu Mar 5 05:31:16 CET 2009


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 Cheyette

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))

More info at http://had.co.nz/plyr

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list