我Dataframe
叫copy1
:
copy1
Source: local data frame [4 x 4]
Groups: GM [2]
GM Avg.Start.Time Avg.Close.Time Avg.Last.Task.Duration
(fctr) (fctr) (fctr) (int)
1 ED 13:15 16:16 181
2 ED 16:12 17:44 92
3 LD 15:32 17:27 115
4 LD 14:38 17:11 153
我想计算Avg.Close.Time
每GM
我试过了:
copy1$Avg.Start.Time <-strptime(copy1$Avg.Start.Time, "%H:%M")
copy1%>%group_by(GM)%>%
summarise(mean(copy1$Avg.Start.Time,na.rm=T))
但是得到这个:
Error: column 'Avg.Start.Time' has unsupported type : POSIXlt, POSIXt
我也尝试过使用lubridate
:
copy1$Avg.Start.Time <- hm(copy1$Avg.Start.Time)
mean(copy1$Avg.Start.Time,na.rm = T)
但是得到“ 0”
有什么想法我该如何计算Avg.Start.Time
Per GM
?
您可以as.POSIXct
用来进行转换,其结果可以用于mean
:
result <- copy1%>%group_by(GM)%>%
summarise(mean(as.POSIXct(Avg.Start.Time, format="%M:%S"),na.rm=T))
但是,这会将当前日期添加到时间中:
print(result)
## A tibble: 2 x 2
## GM mean(as.POSIXct(copy1$Avg.Start.Time,...
## <fctr> <time>
##1 ED 2016-08-24 00:14:54
##2 LD 2016-08-24 00:15:05
正如OP所指出的,我们可以format
得出删除日期的结果:
result <- copy1%>%group_by(GM)%>%
summarise(Avg.Start.Time=format(mean(as.POSIXct(Avg.Start.Time, format="%M:%S"),na.rm=T), format="%M:%S"))
## A tibble: 2 x 2
## GM Avg.Start.Time
## <fctr> <chr>
##1 ED 14:43
##2 LD 15:05
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句