是否有从以下格式计算小时总和的功能:“ H:M:S”

时间:2019-08-25 20:41:38

标签: r

我的工作目标是删除每个小时(1h,2h,3h ........ 23h)都无法达到至少一个温度的日期

  • 我用来通过id和日期计算小时总和
  • 然后我将使用过滤器删除行
sum_heure=tab[,list(h=sum(hour(as.POSIXct(tab$heure1,format="%H:%M:%S")))),by=list(id,date1)]

sub_Test_Heure=filter(sum_heure,h ==276)

这是我的数据框示例:

id<- c("130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f","130f"
       ,"138g","138g","138g")
date1 <- c("01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ", "01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 "
           ,"01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 "
           ,"01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 "
           ,"01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 ","01/01/2017 "
           ,"02/01/2017 ","02/01/2017 ","02/01/2017 ")
temperature <- c(15,16,20,18,26,18,24,24,28,25,27,23,26,19,28,17,26,34,16,18,17,13,28,29,25,27,29,16,19,17,19,26,27,30,34,38,36,35,39,28,26,29,27,24,32,30,23,37,29,32,29,27,29)
heure1<-c("00:04:00","00:14:00","00:30:00","00:50:00","01:04:00","01:24:00","01:34:00","01:50:00","02:00:00","02:14:00","02:34:00","02:54:00","03:10:00","03:20:00","03:54:00","04:34:00","04:59:00","05:04:00","06:00:00","06:20:00","06:44:00","07:14:00","07:34:00","08:04:00","08:44:00","09:00:00","10:04:00"
          ,"10:04:00","10:34:00","10:54:00","11:24:00","12:04:00","13:04:00","14:04:00","14:49:00","15:00:00","15:34:00","16:00:00","16:44:00","17:14:00","17:44:00","18:25:00","19:30:00","20:04:00","20:45:00","21:10:00","22:04:00","22:47:00","23:12:00","23:50:00","00:50:00","01:30:00","22:10:00")
tab <- cbind(id,date1,heure1,  temperature)

2 个答案:

答案 0 :(得分:1)

不要使用完整的数据集列(tab$heure1),而只需指定未加引号的列名(假设它是data.table(请注意,cbind会转换为`matrix)。< / p>

library(data.table)
library(lubridate)
setDT(tab)[, .(h = sum(hour(as.POSIXct(heure1, format = "%H:%M:%S")))), 
           .(id, date1)]
#     id       date1   h
#1: 130f 01/01/2017  479
#2: 138g 02/01/2017   23

或使用hms

setDT(tab)[, .(h = sum(hms(heure1)@hour)), .(id, date1)]

数据

tab <- data.frame(id, date1, heure1, temperature)

答案 1 :(得分:0)

假设您已将数据作为数据帧(而非矩阵)读取,我们可以将unite date1heure1列将其转换为datetime,提取{{1} }和hour中的date,选择具有一天中所有时间(0:23)datetime group_byid的日期,并取{{每个日期的date中的1}}。

sum