R两列之间的时差(以毫秒为单位) [英] R Time difference between 2 columns in milliseconds

查看:85
本文介绍了R两列之间的时差(以毫秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含性能数据的数据集。第1列包含END_DATE,第2列包含START_DATE。
两列中datetime的格式:03-08-16 08:55:59,177000000
我想添加一个新列,用于存储以毫秒为单位的时差。

I have a dataset containing performance data. Column 1 contains the END_DATE and column 2 the START_DATE. The format of the datetime in both columns: 03-08-16 08:55:59,177000000 I want to add a new column where the time difference in milliseconds is stored.

示例:


列1:03-08-16 08:55:59,177000000

column 1: 03-08-16 08:55:59,177000000

列2:03-08-16 08:55:59,888000000

column 2: 03-08-16 08:55:59,888000000

以秒为单位的新列:0,711

new column in seconds: 0,711

我试图转换为as.POSIX ..,但是不知何故,毫秒消失了。有人回答说 OS是格式选项可以解决问题,但我并没有成功:

I tried to convert to as.POSIX.. but somehow the milliseconds gets lost. There was an answer stating that the "OS" format option would do the trick but i wasn't succesfull:


time = as.POSIXct( 03-08-16 08:56:06, 345000000,格式=%d-%m-%y%H:%M:%OS)

time = as.POSIXct("03-08-16 08:56:06,345000000", format="%d-%m-%y %H:%M:%OS")

时间

[1 ]" 2016-08-03 08:56:06 CEST"

[1] "2016-08-03 08:56:06 CEST"

谢谢!

推荐答案

我的评论扩展:

d <- data.frame(col1= '03-08-16 08:55:59,177000000', col2= '03-08-16 08:55:59,888000000')

d2 <- data.frame( lapply(d, sub, patt="[,]", repl="."))
d2
#-------
                         col1                        col2
1 03-08-16 08:55:59.177000000 03-08-16 08:55:59.888000000
#-------
 d3 <- data.frame( lapply(d2, as.POSIXct, format= "%d-%m-%y %H:%M:%OS")  )
 d3
#--------
                 col1                col2
1 2016-08-03 08:55:59 2016-08-03 08:55:59
#-------
 difftime(d3$col1, d3$col2)
#-------
Time difference of -0.711 secs

这篇关于R两列之间的时差(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆