Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R查找YYYY-MM-DD HH:MM:SS.MMM格式的时间差(以秒为单位)_R_Date_Datetime_Time - Fatal编程技术网

R查找YYYY-MM-DD HH:MM:SS.MMM格式的时间差(以秒为单位)

R查找YYYY-MM-DD HH:MM:SS.MMM格式的时间差(以秒为单位),r,date,datetime,time,R,Date,Datetime,Time,我试图减去2个字符向量,其中包含以下格式的日期时间信息: > dput(train2) structure(list(time2 = c("2011-09-01 23:44:52.533", "2011-09-05 12:25:37.42", "2011-08-24 12:56:58.91", "2011-10-25 07:18:14.722", "2011-10-25 07:19:51.697" ), time3 = c("2011-09-01 23:43:59.752", "201

我试图减去2个字符向量,其中包含以下格式的日期时间信息:

> dput(train2)

structure(list(time2 = c("2011-09-01 23:44:52.533", "2011-09-05 12:25:37.42", 
"2011-08-24 12:56:58.91", "2011-10-25 07:18:14.722", "2011-10-25 07:19:51.697"
), time3 = c("2011-09-01 23:43:59.752", "2011-09-05 12:25:01.187", 
"2011-08-24 12:55:13.012", "2011-10-25 07:16:51.759", "2011-10-25 07:16:51.759"
)), .Names = c("time2", "time3"), row.names = c(NA, 5L), class = "data.frame")
我四处搜寻并玩了
zoo
as.Date
as.POSIXct
,试图找到正确的代码减去2个datetime对象,并在几秒钟内得到答案,但运气不佳

如果您有任何建议,我将不胜感激。

简单的豌豆:

R> now <- Sys.time()
R> then <- Sys.time()
R> then - now
Time difference of 5.357 secs
R> class(then - now)
[1] "difftime"
R> as.numeric(then - now)
[1] 5.357
R> 
R>now-then-now
时差为5.357秒
R> 课堂(当时-现在)
[1] “扩散时间”
R> as.numeric(当时-现在)
[1] 5.357
R>
对于您的数据:

R> df
                    time2                   time3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2  2011-09-05 12:25:37.42 2011-09-05 12:25:01.187
3  2011-08-24 12:56:58.91 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2 <- strptime(df$time2, "%Y-%m-%d %H:%M:%OS")
R> df$time3 <- strptime(df$time3, "%Y-%m-%d %H:%M:%OS")
R> df
                    time2                   time3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2 - df$time3
Time differences in secs
[1]  52.781  36.233 105.898  82.963 179.938
attr(,"tzone")
[1] ""
R> 
R>df
时间2时间3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2  2011-09-05 12:25:37.42 2011-09-05 12:25:01.187
3  2011-08-24 12:56:58.91 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2 df$time3 df
时间2时间3
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759
R> df$time2-df$time3
以秒为单位的时差
[1]  52.781  36.233 105.898  82.963 179.938
属性(,“tzone”)
[1] ""
R>
并以数字形式添加回数据框:

R> df$dt <- as.numeric(df$time2 - df$time3)
R> df
                    time2                   time3      dt
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752  52.781
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187  36.233
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012 105.898
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759  82.963
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759 179.938
R> 
R>df$dt df
时间2时间3 dt
1 2011-09-01 23:44:52.533 2011-09-01 23:43:59.752  52.781
2 2011-09-05 12:25:37.420 2011-09-05 12:25:01.187  36.233
3 2011-08-24 12:56:58.910 2011-08-24 12:55:13.012 105.898
4 2011-10-25 07:18:14.722 2011-10-25 07:16:51.759  82.963
5 2011-10-25 07:19:51.697 2011-10-25 07:16:51.759 179.938
R>

非常感谢。我一个人永远不会得到这个。日期时间对我来说是一个“已知未知”的东西(借用唐·拉姆斯菲尔德的话)。也许值得指出的是,你可以使用
difftime(then,now,units=“secs”)
difftime(then,now,units=“min”)
。。。参见
?difftime
@screechOwl:与许多东西一样,它有时需要一些挠头和额头撞击,但它非常非常强大。绝对值得学习。这个答案可能不正确。默认情况下,difftime对象具有
units=“auto”
,因此以最合适的单位显示(类似于最小单位,即单位大小一致且绝对差值大于1)
as.numeric
应使用
units=“secs”
。这不是原始问题和数据集的一部分,因此我(有点强烈)不同意该评论(并投反对票)。
>  x1<-"2013-03-03 23:26:46.315" 
>  x2<-"2013-03-03 23:31:53.091"
>  x1 <- strptime(x1, "%Y-%m-%d %H:%M:%OS")
>  x2 <- strptime(x2, "%Y-%m-%d %H:%M:%OS")
> x1
[1] "2013-03-03 23:26:46"
> x2
[1] "2013-03-03 23:31:53"
op <- options(digits.secs = 3)
> as.numeric(x2-x1,units="secs")
[1] 306.776