[R] Failure to aggregate

David Winsemius dwinsemius at comcast.net
Mon Sep 6 20:01:00 CEST 2010


On Sep 6, 2010, at 12:15 PM, Dimitri Shvorob wrote:

>
> I have a (very big - 1.5 rows) dataframe with a (POSIXt"  "POSIXlt")  
> column h
> (hour). Surprisingly, I cannot calculate a simple aggregate over the
> dataframe.
>
>> n.h1 = sqldf("select distinct h, count(*) from x group by h")
> Error in sqliteExecStatement(con, statement, bind.data) :
>  RS-DBI driver: (error in statement: no such table: x)
> In addition: Warning message:
> In value[[3L]](cond) : RAW() can only be applied to a 'raw', not a  
> 'double'
>
>> n.h2 = aggregate(x$price, by = x$h, FUN = nrow)

A vector argument (x$price) would only have one row (at most).

nrow(c(1,2)
NULL

> Error in names(y) <- c(names(by), names(x)) :
>  'names' attribute [10] must be the same length as the vector [2]

Try:

tapply(x$price, by = x$h, FUN = length)

-- 
David.



More information about the R-help mailing list