在R中如何找到时间差(处理文件所需的时间)? [英] How to find the difference of time (Time taken to process a file) in R?
问题描述
我有日志数据,它记录了开始日期和结束日期时间戳。
I have Log data, it records the Start datetime and end datetime stamp.
日志文件中的数据如下所示
excel中的预置数据
Data from the log file look as below Preapred data in excel
Start_Date1 Start_Time1 Start_Millisecond1 Start_Date2 Start_Time2 Start_Millisecond2
29-11-2015 18:25:04 671 29-11-2015 18:40:05 275
29-11-2015 18:25:03 836 29-11-2015 18:40:04 333
10-11-2015 02:41:57 286 10-11-2015 02:51:52 690
当我使用Rstudio将数据加载到R中时。数据类型如下所示。
When i load the data into R using Rstudio. The class of data looks as below.
我正在使用下面的代码行将日期转换为POSIXlt。
I am using below line of code to convert date to POSIXlt.
nov $ Start.Date1< -as.POSIXlt(as.character(nov $ Start.Date1),format =%d-%m-%Y)
nov <-read.csv(file = '././data/Data For R Nov CBEFF log.csv',header = TRUE,na.strings = FALSE,stringsAsFactors = FALSE)
str(nov $开始.Time1)
str(nov$Start.Time1)
nov $ Start.Date1< -as.POSIXlt(as.character(nov $ Start.Date1),format =%d-%m-% Y)
nov$Start.Date1<-as.POSIXlt(as.character(nov$Start.Date1), format="%d-%m-%Y")
nov $ Start.Time1< -as.POSIXlt(as.character(nov $ Start.Time1),format =%H:%M:% S)
nov $ Start.Time1< -format(nov $ Start.Time1,format =%H:%M:%S)
nov$Start.Time1<-as.POSIXlt(as.character(nov$Start.Time1), format="%H:%M:%S") nov$Start.Time1<-format(nov$Start.Time1, format="%H:%M:%S")
nov $ Start.Date2< -as.POSIXlt(as.character(nov $ Start.Date2),format =%d-%m-%Y)
nov$Start.Date2<-as.POSIXlt(as.character(nov$Start.Date2), format="%d-%m-%Y")
nov $ Start.Time2< -as.POSIXlt(as.character(nov $ Start.Time2),format =%H: %M:%S)
nov $ Start.Time2< -format(nov $ Start.Time2,format =%H:%M:%S)
nov$Start.Time2<-as.POSIXlt(as.character(nov$Start.Time2), format="%H:%M:%S") nov$Start.Time2<-format(nov$Start.Time2, format="%H:%M:%S")
**
**
StartTime1和StartTime2现在是chr数据类型。
StartTime1 and StartTime2 are now in chr data type.
推荐答案
这应该是诀窍。如果你发布了数据(可重现的例子),我可以检查代码。这样它可能会有一些打字错误。
This should do the trick. If you had posted the data (reproducible example), I could check the code. This way it might have some typos in it.
nov<-read.delim("sample.csv", sep=";", dec=".")
nov$start1<-as.POSIXlt(paste(nov$Start_Date1,nov$Start_Time1 ,sep=" "), format="%d-%m-%Y %H:%M:%S")
nov$start2<-as.POSIXlt(paste(nov$Start_Date2,nov$Start_Time2 ,sep=" "), format="%d-%m-%Y %H:%M:%S")
nov$timediff<-as.numeric(difftime(nov$start2,nov$start1, unit="secs"))*1000+(nov$Start.Milisecond2-nov$Start.Milisecond1)
这给你一个毫秒的时间
编辑
通过样本数据验证。变量名称已从Start.Date1更改为Start_Date1
EDIT Verified with sample data. The variable names have changed from "Start.Date1" to "Start_Date1"
这篇关于在R中如何找到时间差(处理文件所需的时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!