以%H:%M:%S格式计算时差,格式为R
我试图计算满足特定条件的行的两列之间的时间差以%H:%M:%S格式计算时差,格式为R,r,datetime,datetime-format,difference,R,Datetime,Datetime Format,Difference,我试图计算满足特定条件的行的两列之间的时间差 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
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).
dates1喜欢这件作品吗
with(DS, ifelse(site == 18, as.character(lubridate::seconds_to_period(
difftime(dates2, dates1, units = "secs"))),
format(dates1, "%T")))
#[1] "12:20:00" "15:30:00" "44M 0S" "1H 13M 0S" NA
您可以使用chron库中的“times”格式
(这不是很简单,但似乎有效)
#以分钟计算差异
DS$Time1NEW您可以在小时内使用difftime
<代码>楼层
和%%1
中的每个小时和分钟都可以粘贴在一起。不过,还需要一些舍入
DS$output[DS$site == 18] <- {
tmp <- as.numeric(round(with(DS[DS$site == 18, ], difftime(dates2, dates1, units="hours")), 2))
paste(sprintf("%02d", floor(tmp)), round(tmp %% 1 * 60), "00", sep=":")
}
DS$output[site != 18] <- strftime(DS$dates1[DS$site != 18], "%T")
DS
# site dates1 dates2 output
# 1 15 1899-12-31 12:20:00 1899-12-31 11:13:00 12:20:00
# 2 16 1899-12-31 15:30:00 1899-12-31 11:41:00 15:30:00
# 3 18 1899-12-31 13:20:00 1899-12-31 14:04:00 00:44:00
# 4 18 1899-12-31 11:50:00 1899-12-31 13:03:00 01:13:00
# 5 15 <NA> 1899-12-31 13:18:00 <NA>
DS$output[DS$site==18]您可以使用hms
库
require(hms)
x <- data.frame(TIME_SPENT = as.difftime(runif(10) * 10, format = 'H', units = 'hours'))
x$TIME_SPENT_HMS <- hms(hours = as.numeric(x$TIME_SPENT))
> x
TIME_SPENT TIME_SPENT_HMS
1 0.670335 hours 00:40:13.206023
2 6.740590 hours 06:44:26.122495
3 9.433242 hours 09:25:59.670743
4 9.350243 hours 09:21:00.873019
5 1.459504 hours 01:27:34.214544
6 4.820711 hours 04:49:14.560171
7 5.052186 hours 05:03:07.870653
8 9.415136 hours 09:24:54.489527
9 4.717802 hours 04:43:04.086949
10 4.131969 hours 04:07:55.087836
您可以对其执行各种计算
> x$TIME_SPENT_HMS[1] + 10
Time difference of 2423.206 secs
表视图中显示的值也用分号表示:
您是否考虑过lubridate
软件包?在中,我找不到以H%:%M:%S格式计算时间差的函数lubridate@RonakShah我刚刚更新了示例以包含预期的输出。我加入了一个较小的数据集,以便于理解。谢谢,但是如何将“4400”转换为00:44:00?我需要他们在相同的格式做一些分析后,数据集是big@AnaAntunes如果您想对它们进行分析,这两种格式都没有用处,因为它们只是字符串。如果您希望所有内容都采用相同的格式,那么更改日期1的格式如何<代码>带有(DS,ifelse(site==18,as.character)(lubridate::seconds\u to_period(difftime(dates2,dates1,units=“secs”)),格式(dates1,“%HH%MM%SS”))
是日期的格式完全相同。但是如果我想从“%HH%MM%SS”更改为“%H:%M:%S”,我该怎么做呢?如果我使用格式(DS$output,“%H:%M:%S”),但它给了我一个错误:格式错误。默认值(其他站点$Time1NEW,“%H:%M:%S”):无效的“修剪”参数这也是一个不错的方法。谢谢
require(hms)
x <- data.frame(TIME_SPENT = as.difftime(runif(10) * 10, format = 'H', units = 'hours'))
x$TIME_SPENT_HMS <- hms(hours = as.numeric(x$TIME_SPENT))
> x
TIME_SPENT TIME_SPENT_HMS
1 0.670335 hours 00:40:13.206023
2 6.740590 hours 06:44:26.122495
3 9.433242 hours 09:25:59.670743
4 9.350243 hours 09:21:00.873019
5 1.459504 hours 01:27:34.214544
6 4.820711 hours 04:49:14.560171
7 5.052186 hours 05:03:07.870653
8 9.415136 hours 09:24:54.489527
9 4.717802 hours 04:43:04.086949
10 4.131969 hours 04:07:55.087836
> class(x$TIME_SPENT_HMS)
[1] "hms" "difftime"
> x$TIME_SPENT_HMS[1] + 10
Time difference of 2423.206 secs