[英]Time difference between two times in R
I am trying to get the difference between two times in R. 我试图在R中获得两次之间的差异。
For example, time difference between two times: "03:15" and "01:40" will be 1 hour 35 minutes. 例如,两次“ 03:15”和“ 01:40”之间的时差将为1小时35分钟。
I tried the following code in R: 我在R中尝试了以下代码:
difftime("03:15", "01:40", tz="", units = "secs")
But I am getting the following error: 但是我收到以下错误:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
Any help will be highly appreciated. 任何帮助将不胜感激。
You can use strptime
to convert to POSIXct
: 您可以使用strptime
转换为POSIXct
:
t0 <- "03:15";
t1 <- "01:40";
# Time difference in seconds
difftime(strptime(t0, format = "%H:%M"), strptime(t1, format = "%H:%M"), units = "secs");
#Time difference of 5700 secs
# Time difference in minutes
difftime(strptime(t0, format = "%H:%M"), strptime(t1, format = "%H:%M"), units = "mins");
#Time difference of 95 mins
You could try to convert your characters in the POSIXct format first: 您可以先尝试将字符转换为POSIXct格式:
difftime(as.POSIXct("03:15",format="%H:%M"),
as.POSIXct("01:40",format="%H:%M"),
tz="", units = "mins")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.