0

I have Log data, it records the Start datetime and end datetime stamp.

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

When i load the data into R using Rstudio. The class of data looks as below.

Data Loaded and Its data type

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$Start.Time1)

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.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")

**

> I want to caluclate time taken to complete that is > StartTime2-StartTime1

**

StartTime1 and StartTime2 are now in chr data type.

sandeep
  • 31
  • 2
  • 9
  • 1
    Have a look at the [`lubridate` package](https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html). – Axeman Feb 25 '16 at 10:37
  • I have looked into it but unable to get exactly what i wanted.. – sandeep Feb 25 '16 at 10:43
  • 1
    You are much more likely to get help if you show us what your have tried and why it didn't work. You should also provide [a minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) instead of screenshots. – Axeman Feb 25 '16 at 10:47

1 Answers1

1

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)

This gives you the time in miliseconds

EDIT Verified with sample data. The variable names have changed from "Start.Date1" to "Start_Date1"

Buggy
  • 2,050
  • 21
  • 27
  • Startdate1 and Startdate2 are just dates there is not time in it... the column next to startdate1 that is starttime1. i need to find the difference of that with Starttime2 next column to Startdate2..I have linked the data in the question – sandeep Feb 25 '16 at 10:55
  • That's why there is a paste operation in there :D That should take care of the missing time... As for the reproducible example: You just added a screenshots (which is nice to have), but having a subset of the data (e.g. dput or a csv) would be more helpful. – Buggy Feb 25 '16 at 10:59
  • I verified with your sample data. Apart from the colnames change, there was a little typo in the second line. But now it works. There might of course be nicer solutions but this does the trick. – Buggy Feb 25 '16 at 12:33
  • What is the return value of `paste(nov$Start_Date1,nov$Start_Time1 ,sep=" ")` ? Are you sure that it is not a problem with the names of your columns (because your example has different names) – Buggy Feb 25 '16 at 13:42
  • Bro Every thing is perfect . One small correction need to be done in code. Corrected code. `nov$timediff<-as.numeric(difftime(nov$Start2,nov$Start1,units = "secs"))+((nov$Start.Millisecond2-nov$Start.Millisecond1)/1000)` Instead of multiplying 1000 to seconds it should be multiplying to Milliseconds. I have checked as answer thanks for your time and support – sandeep Feb 25 '16 at 13:58