2016-09-23 68 views
1

我试图确定给每个服务器的最大(使用)列每天的用电高峰小时高峰使用一个数据帧中提取高峰日期时间:你如何从给定的每日期

DATE DATETIME   Server MACH Capacity Used 
9/16/2016 9/16/2016 12:00 cpu1 A 22256 17939 
9/16/2016 9/16/2016 13:00 cpu1 A 22256 12591 
9/16/2016 9/16/2016 14:00 cpu1 A 22256 15834 
9/16/2016 9/16/2016 15:00 cpu2 B 22256 14095 
9/16/2016 9/16/2016 16:00 cpu2 B 22256 18186 
9/16/2016 9/16/2016 17:00 cpu2 B 22256 12637 

在这数据帧,为日期9/16/2016为cpu1,最大使用是17939,它发生在2016年9月16日12:00

我想使用data.table包,并可以选择最大使用喜欢此:

df<-data.table(df) 
df<-df[,peak_used:=max(Used), by=c("Server","DATE")] 

I还需要提取高峰时段并创建一个新列作为高峰列,并在那里插入日期和时间?

任何想法如何提取峰值DATETIME for max用于该DATE?

+2

你可以试试'DF [,peaktime:= DATETIME [which.max(二手)],由=。 (服务器,日期)]' – akrun

回答

3

我们可以使用which.max得到'服务器','DATE'子集'DATETIME'使用该索引后的最大行数索引,并通过确定(:=)值来创建'peaktime'它

df[, peaktime := DATETIME[which.max(Used)], by = .(Server, DATE)] 

如果我们既需要 'peakused' 和 'peaktime',然后

df[, c("peakused", "peaktime") := { 
     i1 <- which.max(Used) 
     .(DATETIME[i1], Used[i1])}, by = .(Server, DATE)] 
+0

而不是使用which.max,是否有可能使用which.quantile,我喜欢lo好吧,在第95百分点。 – user1471980

+0

@ user1471980没有'which.quantile'功能。 – akrun

相关问题