r - 计算 %H 中的时间差 :%M:%S format in R

标签 r datetime datetime-format difference

我正在尝试计算满足特定条件的行的 2 列之间的时间差。

dates1 <- c("1899-12-31 12:20:00 PMT", "1899-12-31 15:30:00 PMT", "1899-12-31 13:20:00 PMT", "1899-12-31 11:50:00 PMT",
             NA)   

dates2 <- c("1899-12-31 11:13:00 PMT", "1899-12-31 11:41:00 PMT", "1899-12-31 14:04:00 PMT", "1899-12-31 13:03:00 PMT", 
            "1899-12-31 13:18:00 PMT")

site <- c(15, 16, 18, 18,
          15)

DS <- as.data.frame(cbind(site, dates1 , dates2))

## convert to POSIXct format
DS[, 2:3] <- lapply(DS[, 2:3], function(x) as.POSIXct(strptime(x,"%Y-%m-%d %H:%M:%S ",tz="")))

## create a new columns with the time difference between dates1 and dates2 in %H:%M:%S format if site=18 , else take value from column dates1
DS$output <- ifelse(DS$site ==18, 
                      difftime(DS$dates1, DS$dates2, units = "mins"),
                      DS$dates1) 

## using the code above doesnt work because:
#1- using the difftime function I can't choose the "%H:%M:%S" for the output
#2- for sites != 18, the dates1 lose its "%H:%M:%S format


# I have also tried to use:
DS$output <- ifelse(DS$site ==18, 
                      difftime(DS$dates1, DS$dates2, units = "mins"),
                      0) 
## and then convert the difference in minutes to %H:%M:%S format using
DS$output<- as.difftime(DS$output , format = "%H:%M:%S", units = "mins")

## but it doesnt work.

## the output should be something like:

output<- c("12:20:00", "15:30:00","00:44:00", "01:13:00", NA)

DS.out <- cbind(DS, output)

> DS.out 
  site                  dates1                  dates2   output
1   15 1899-12-31 12:20:00 PMT 1899-12-31 11:13:00 PMT 12:20:00
2   16 1899-12-31 15:30:00 PMT 1899-12-31 11:41:00 PMT 15:30:00
3   18 1899-12-31 13:20:00 PMT 1899-12-31 14:04:00 PMT 00:44:00
4   18 1899-12-31 11:50:00 PMT 1899-12-31 13:03:00 PMT 01:13:00
5   15                    <NA> 1899-12-31 13:18:00 PMT     <NA>

    #Where output is the time difference calculated for rows 3 and 4 (site=18) 
#or a copy of the time from dates 1 for the other rows (with sites different from 18). 

无论如何,是否可以使用不同的函数以 H%:%M:%S 格式计算时差?

最佳答案

您可以使用 chron 库中的“时间”格式。

(这不是很简单,但似乎有效)

# get the difference in minutes
DS$Time1NEW <- ifelse(DS$site ==18, 
                      difftime(DS$dates1, DS$dates2, units = "mins"),
                      NA) 

# get the absolute value
DS$Time1NEW<-abs(DS$Time1NEW)

# conver to hours:min:sec using "times" from library chron
DS$hours_minutes<-times((DS$Time1NEW%/%60 +  DS$Time1NEW%%60 /60)/24)

# convert the others (not in site 18)
DS$no18<-times(paste0(hours(DS$dates1),':', minutes(DS$dates1),':', seconds(DS$dates1)))

# create final data column
DS$final<-DS$hours_minutes

# now substitute when necessary
wh<-which(is.na(DS$final))
DS$final[wh]<-DS$no18[wh]

这就是你得到的:
> head(DS)
  site                  dates1                  dates2 Time1NEW hours_minutes     no18    final
1   15 1899-12-31 12:20:00 PMT 1899-12-31 11:13:00 PMT       NA          <NA> 12:20:00 12:20:00
2   16 1899-12-31 15:30:00 PMT 1899-12-31 11:41:00 PMT       NA          <NA> 15:30:00 15:30:00
3   18 1899-12-31 13:20:00 PMT 1899-12-31 14:04:00 PMT       44      00:44:00 13:20:00 00:44:00
4   18 1899-12-31 11:50:00 PMT 1899-12-31 13:03:00 PMT       73      01:13:00 11:50:00 01:13:00
5   15                    <NA> 1899-12-31 13:18:00 PMT       NA          <NA>     <NA>     <NA>

关于r - 计算 %H 中的时间差 :%M:%S format in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61102608/

相关文章:

R - 数据框列表 - 计算并添加新行

c# - 如何在 .Net 中正式正确解析 ISO8601 日期时间?

mysql - 处理数据库中不同的日期时间格式?

.net - 当且仅当时间部分 = 00 :00:00? 时,如何在显示中抑制 .NET DateTime 的时间部分

Java 8 时间转换器从 12 到 24 格式

java - 使用时区信息转换日期时间(以毫秒为单位)

r - 如何将逆高斯分布拟合到我的数据中,最好使用 fitdist {fitdistrplus}

r - 构建 R 包, "later,"生成 undefined symbol

r - 如何控制 RMarkdown/knitr 加载的默认包以避免选项冲突

javascript Date 对象转换本地时区