[R] How to group data by day
Michela Ferron
ferron at fbk.eu
Wed Feb 16 14:05:39 CET 2011
Many thanks Mikhail,
aggregate() is fine!
Il 14/02/2011 20.42, Mikhail Titov ha scritto:
> It depends what would you like to get at the end. Perhaps you don't
> necessary need this type of numbering. For instance, if you'd like to
> calculate daily average.
>
> london$id<- as.Date(london$id)
>
> For sum by day you could use, let's say, this
>
> aggregate(words~id,london,FUN=sum)
>
> If you really want what you've asked:
>
> london$one=1
> u=unique(london$id)
> z=aggregate(one~id,london,FUN=sum)
> london$day=rep(seq(along.with=z$one),z$one)
>
> Mikhail
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Michela Ferron
>> Sent: Monday, February 14, 2011 11:09 AM
>> To: r-help at r-project.org
>> Subject: [R] How to group data by day
>>
>> Hi everybody,
>>
>> I'm a beginner in R and I'm having a hard time grouping my data by day.
>> The data are in this format:
>>
>> id; words
>> 2005-07-07T09:59:56Z; 35
>> 2005-07-07T10:01:39Z; 13
>> 2005-07-08T10:02:22Z; 1
>> 2005-07-09T10:03:16Z; 23
>> 2005-07-10T10:04:23Z; 39
>> 2005-07-10T10:04:39Z; 15
>>
>> I've transformed the date strings in dates with the function:
>> london$id<- transform(london$id, as.Date(london$id, format="%Y-%m-
>> %d%T%H:%M:%S%Z"))
>> and it seems to work.
>>
>> Now I would like to add a new "day" variable to group data by day, like
>> this:
>>
>> id; words; day
>> 2005-07-07T09:59:56Z; 35; 1
>> 2005-07-07T10:01:39Z; 13; 1
>> 2005-07-08T10:02:22Z; 1; 2
>> 2005-07-09T10:03:16Z; 23; 3
>> 2005-07-10T10:04:23Z; 39; 4
>> 2005-07-10T10:04:39Z; 15; 4
>>
>> How can I do that?
>>
>> Many thanks!
>>
>> Michela
>>
>> ______________________________________________
>> R-help at 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.
More information about the R-help
mailing list