1
votes

I have hourly data that I would like to take the max out of hourly data and report what time it occurred.

this is my data frame:

dput(head(monthly_cpu,24))

structure(list(name = c("Daily-Peaks", "Daily-Peaks", "Daily-Peaks", 
"Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", 
"Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", 
"Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", 
"Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", "Daily-Peaks", 
"Daily-Peaks"), date = structure(c(1315828800, 1315832400, 1315836000, 
1315839600, 1315843200, 1315846800, 1315850400, 1315854000, 1315857600, 
1315861200, 1315915200, 1315918800, 1315922400, 1315926000, 1315929600, 
1315933200, 1315936800, 1315940400, 1315944000, 1315947600, 1316001600, 
1316005200, 1316008800, 1316012400), class = c("POSIXct", "POSIXt"
), tzone = ""), cpu = c(5.6, 7.68, 8.64, 10.4, 11.36, 12, 12.16, 
12.8, 13.28, 13.92, 7.2, 7.84, 9.28, 10.72, 11.04, 11.04, 10.56, 
11.36, 10.72, 10.88, 1.76, 5.76, 9.6, 10.88), day = structure(c(15229, 
15229, 15229, 15229, 15229, 15229, 15229, 15229, 15229, 15229, 
15230, 15230, 15230, 15230, 15230, 15230, 15230, 15230, 15230, 
15230, 15231, 15231, 15231, 15231), class = "Date"), max = c(13.92, 
13.92, 13.92, 13.92, 13.92, 13.92, 13.92, 13.92, 13.92, 13.92, 
11.36, 11.36, 11.36, 11.36, 11.36, 11.36, 11.36, 11.36, 11.36, 
11.36, 12.48, 12.48, 12.48, 12.48)), .Names = c("name", "date", 
"cpu", "day", "max"), row.names = c(NA, 24L), class = "data.frame")

I create another field called day and get the max value per that day with the data.table package as follows:

monthly_cpu$day<-as.Date(monthly_cpu$date)
monthly_cpu<-data.table(monthly_cpu)
monthly_cpu<-monthly_cpu[,max:=max(cpu), by=day]

at this point I need to pick the date (which is as.POSIXct format) and the max value for each day.

I need my final monthly_cpu df frame to be like this:

Date   Max
2013-04-09 08:00:00 67.00
2013-04-10 13:00:00 50.00
2013-04-11 09:00:00 88.00
2013-04-12 12:00:00 100.00
2013-04-13 15:00:00 10.00

etc

Is there a way to pick the date and the max value from the monthly_cpu data frame and how?

2

2 Answers

2
votes

Sounds like instead of the assignment in your last step, you want to do:

monthly_cpu[, max(cpu), by=day]
0
votes

I am sure there is a slick way of doing this, but I think this will work for me:

monthly_cpu<-subset(monthly_cpu, cpu == max)