[R] Aggregating data -- table almost does it

Thomas Lumley tlumley at u.washington.edu
Thu Oct 10 16:24:20 CEST 2002


On 10 Oct 2002, Mike Nielsen wrote:

> Now, I would like to (for example) count the number of events (or maybe
> find the mean of a certain variable) in each hour of each day.  To
> count, I could use:
>
> > table(as.character(dates(fred$dates.chron)),hours(fred$dates.chron))
>
>            10 11  12   13 14
>   09/19/02  3 63 112 3042 23
>
> (there is, so far, only one day's worth of events in the data frame).
>
> To do fancier stuff, I know I could use aggregate.
>
> However, I only get a column for each hour in which there was an event.
> What I would dearly love to have is 24 columns, with 0 (zero) indicating
> that there were no events in that hour of that day.
>

You can define a factor with 24 levels, then they will all be used in
table
> table(data)
data
 1  2  3  4  5  6  7  8  9 10
 3  7 14 22 16 12 13  5  2  6
> table(factor(data,levels=1:24))

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 3  7 14 22 16 12 13  5  2  6  0  0  0  0  0  0  0  0  0  0  0  0  0  0


	-thomas


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