计算R中的时差 [英] calculating time difference in R
本文介绍了计算R中的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
start.date start.time end.date end.time
1 2012 -07-13 15:01:32 2012-07-13 15:02:42
2 2012-07-05 18:26:31 2012-07-05 18:27:19
3 2012 -07-14 20:23:21 2012-07-14 20:24:11
4 2012-07-29 16:09:54 2012-07-29 16:10:48
5 2012 -07-21 14:58:32 2012-07-21 15:00:17
6 2012-07-04 15:36:31 2012-07-04 15:37:11
7 2012 -07-22 18:28:31 2012-07-22 18:28:50
8 2012-07-09 21:08:42 2012-07-09 21:09:02
9 2012 -07-05 09:44:52 2012-07-05 09:45:05
10 2012-07-02 18:50:47 2012-07-02 18:51:38
我需要计算start.time和end.time之间的差异。
我使用以下代码:
mbehave11 $ diff.time< - difftime(mbehave11 $ end.time,mbehave11 $ start.time,units =secs)
但是我收到这个错误:
as.POSIXlt.character(x,tz,...)中的错误)
/ pre>
字符串不是标准的明确格式
另外:警告消息:
1:在is.na.POSIXlt(strptime(xx,f < - %Y- %m-%d%H:%M:%OS,tz = tz)):
达到1535Mb的总分配:请参阅help(memory.size)
解决方案您必须将您的字符串转换为日期对象,然后才能进行日期/时间算术。尝试这样:
a)阅读数据:
R> dat< - read.table(textConnection(start.date start.time end.date end.time
2012-07-13 15:01:32 2012-07-13 15:02:42
2012-07-05 18:26:31 2012-07-05 18:27:19
2012-07-14 20:23:21 2012-07-14 20:24:11),header =真的)
b)工作在一个观察:
R> strptime(粘贴(dat [,1],dat [,2]),%Y-%m-%d%H:%M:%S)
[1]2012-07-13 15 :01:322012-07-05 18:26:312012-07-14 20:23:21
c)处理集合,转换为数字:
R> as.numeric(difftime(strptime(paste(dat [,1],dat [,2]),%Y-%m-%d%H:%M:%S),
strptime (dat [,3],dat [,4]),%Y-%m-%d%H:%M:%S)))
[1] -70 -48 -50
R>
I have a data with more that 3 million records having start.time and end.time as two of the variables. The first 10 obs are as follows:
start.date start.time end.date end.time 1 2012-07-13 15:01:32 2012-07-13 15:02:42 2 2012-07-05 18:26:31 2012-07-05 18:27:19 3 2012-07-14 20:23:21 2012-07-14 20:24:11 4 2012-07-29 16:09:54 2012-07-29 16:10:48 5 2012-07-21 14:58:32 2012-07-21 15:00:17 6 2012-07-04 15:36:31 2012-07-04 15:37:11 7 2012-07-22 18:28:31 2012-07-22 18:28:50 8 2012-07-09 21:08:42 2012-07-09 21:09:02 9 2012-07-05 09:44:52 2012-07-05 09:45:05 10 2012-07-02 18:50:47 2012-07-02 18:51:38
I need to calculate the difference between start.time and end.time.
I used the following code:
mbehave11$diff.time <- difftime(mbehave11$end.time, mbehave11$start.time, units="secs")
But I am getting this error:
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format In addition: Warning messages: 1: In is.na.POSIXlt(strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz)) : Reached total allocation of 1535Mb: see help(memory.size)
解决方案You must turn your strings into date objects before you can do date/time arithmetic. Try this:
a) Reading your data:
R> dat <- read.table(textConnection("start.date start.time end.date end.time 2012-07-13 15:01:32 2012-07-13 15:02:42 2012-07-05 18:26:31 2012-07-05 18:27:19 2012-07-14 20:23:21 2012-07-14 20:24:11"), header=TRUE)
b) Working on one observation:
R> strptime( paste(dat[,1], dat[,2]), "%Y-%m-%d %H:%M:%S") [1] "2012-07-13 15:01:32" "2012-07-05 18:26:31" "2012-07-14 20:23:21"
c) Working on the set, converting to numeric:
R> as.numeric(difftime(strptime(paste(dat[,1],dat[,2]),"%Y-%m-%d %H:%M:%S"), strptime(paste(dat[,3],dat[,4]),"%Y-%m-%d %H:%M:%S"))) [1] -70 -48 -50 R>
这篇关于计算R中的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文