Junior R
Junior R

Reputation: 93

R max value over a series of days

I'm trying to use R to find the max value of each day for 1 to n days. My issue is there are multiple values in each day. Here is my code. After I run it, it shows same answer for each day:

20130311   12
20130311   12

In the earlier post, I was suggested to use the following approach

library(plyr)
ddply(data,.(Day),summarize,Time=Time[which.max(Value)],max.value=max(Value)) 

My data is as follows:

Day           Time              Value 
20130310     09:30:00             5      
20130310     09:31:00             1 
20130310     09:32:00             2
20130310     09:33:00             3
20130311     09:30:00             0
20130311     09:31:00             12
20130311     09:32:00             1
20130311     09:33:00             5

The solution for this was provided as:

 day         time      value
 20130310    09:30:00    5 
 20130311   09:31:00   12

Any suggestions other than using this approach?

Upvotes: 1

Views: 1038

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

You can use data.table:

DT[, max(Value), by=Date]
#        Date V1
# 1: 20130310  5
# 2: 20130311 12

Where,

library(data.table)
DT <- data.table( theData )

Upvotes: 3

Related Questions