[英]Subtracting time duration from a timestamp in R
I have a data frame as follows with a Session end time (%D/%M/%Y %H:%M:%S) and Session Duration (%M:%S).我有一个数据框如下,结束时间为 Session (%D/%M/%Y %H:%M:%S) 和持续时间为 Session (%M:%S)。 I want to obtain the Session start time in the format %D/%M/%Y %H:%M, by subtracting the Session Duration from the Session End time and attaching it to the data frame as a separate column called "SessionEnd".
我想以 %D/%M/%Y %H:%M 的格式获取 Session 开始时间,方法是从 Session 结束时间中减去 Session 持续时间并将其作为名为“SessionEnd”的单独列附加到数据框. How am I supposed to do this in R?
我应该如何在 R 中执行此操作?
SessionEnd <- c("22/06/2022 0:01:00", "22/06/2022 0:09:00", "22/06/2022 0:10:00", "22/06/2022 0:11:00", "22/06/2022 0:14:00")
Session_Duration <- c("6:11", "8:22", "9:59", "8:09", "4:55")
df <- data.frame(SessionEnd, Session_Duration)
The output should look like the one below. output 应如下所示。
Session_End![]() |
Session_Duration![]() |
Session_Start![]() |
---|---|---|
22/06/2022 0:01:00 ![]() |
6:11 ![]() |
21/06/2022 23:54:49 ![]() |
22/06/2022 0:09:00 ![]() |
8:22 ![]() |
22/06/2022 00:00:38 ![]() |
22/06/2022 0:10:00 ![]() |
9:59 ![]() |
22/06/2022 00:00:01 ![]() |
22/06/2022 0:11:00 ![]() |
8:09 ![]() |
22/06/2022 00:02:51 ![]() |
22/06/2022 0:14:00 ![]() |
4:55 ![]() |
22/06/2022 00:09:05 ![]() |
It would be easy to do this with functions from lubridate
package -使用
lubridate
package 中的函数很容易做到这一点 -
library(dplyr)
library(lubridate)
df %>%
mutate(SessionEnd = dmy_hms(SessionEnd),
Session_Duration = ms(Session_Duration),
SessionStart = SessionEnd - Session_Duration) #%>%
#If you want to maintain the same format as input add the below line
#mutate(across(c(SessionEnd, SessionStart), format, '%d/%m/%Y %H:%M:%S'))
# SessionEnd Session_Duration SessionStart
#1 2022-06-22 00:01:00 6M 11S 2022-06-21 23:54:49
#2 2022-06-22 00:09:00 8M 22S 2022-06-22 00:00:38
#3 2022-06-22 00:10:00 9M 59S 2022-06-22 00:00:01
#4 2022-06-22 00:11:00 8M 9S 2022-06-22 00:02:51
#5 2022-06-22 00:14:00 4M 55S 2022-06-22 00:09:05
You can convert the duration into seconds and subtract them.您可以将持续时间转换为秒并减去它们。
strptime(df$SessionEnd, '%d/%m/%Y %T') -
colSums(t(type.convert(
do.call(rbind, strsplit(df$Session_Duration, ':')), as.is=TRUE))*c(60, 1))
# [1] "2022-06-21 23:54:49 CEST" "2022-06-22 00:00:38 CEST"
# [3] "2022-06-22 00:00:01 CEST" "2022-06-22 00:02:51 CEST"
# [5] "2022-06-22 00:09:05 CEST"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.