[R] Group by

Linlin Yan yanlinlin82 at gmail.com
Thu Mar 11 13:51:26 CET 2010


> DF
        V1  V2   V3
1 10:03:13 3.4 1002
2 10:03:14 5.6 1001
3 10:05:27 7.2  999
4 10:05:33 8.2  998
> DF2 <- t(sapply(split(DF[,-1], gsub('(.{5}).*', '\\1:00', DF$V1)), colSums))
> data.frame(V1 = rownames(DF2), DF2)
               V1   V2   V3
10:03:00 10:03:00  9.0 2003
10:05:00 10:05:00 15.4 1997


On Thu, Mar 11, 2010 at 8:16 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try this:
>
> DF <- read.csv(textConnection("V1,V2,V3
> 10:03:13,3.4,1002
> 10:03:14,5.6,1001
> 10:05:27,7.2,999
> 10:05:33,8.2,998"), header = TRUE)
>
> tDates <- strptime(DF$V1, "%H:%M:%S")
> tDates[[2]] <- floor(strptime(DF$V1, "%H:%M:%S")[[2]] / 5) * 5
>
> aggregate(DF[,c('V2', 'V3')],
>          list(format(tDates, "%H:%M:00")),
>          FUN = sum)
>
> On Thu, Mar 11, 2010 at 6:48 AM, ManInMoon <xmoon2000 at googlemail.com> wrote:
>>
>> I have a matrix with a POSIXct as a numeric in the first column.
>>
>> I would like to create a new matrix that is "grouped by" my chosed time
>> bars.
>>
>> i.e. So I would like to group by hour or day or 5 days, and have all my
>> columns be summed or averaged or counted..
>>
>> mydata:
>>
>> V1,V2,V3
>> 10:03:13,3.4,1002
>> 10:03:14,5.6,1001
>> 10:05:27,7.2,999
>> 10:05:33,8.2,998
>>
>> I want to convert this into say 5 minutes "bars" and sum columns
>>
>> mynewdata:
>>
>> V1,V2,V3
>> 10:00:00,9.0,2003
>> 10:05:00,15.4,1997
>>
>>
>> How can I do this?
>> --
>> View this message in context: http://n4.nabble.com/Group-by-tp1588694p1588694.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> 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