0

I have following 2 datasets VechicleParking_InTime

Vehicle_Id  12/31/15        12/30/15        12/29/15        12/27/15 
    2268    12/31/15 10:26  12/30/2015  14:00:00    12/29/15 11:00  NA
    2269    12/31/15 11:00  12/30/15 10:00  12/29/15 10:00  NA
    2270    12/31/15 0:00   12/30/15 7:00   12/29/15 7:00   NA
    2271    12/31/15 11:00  12/30/15 1:00   12/29/15 8:00   NA
    2272    12/31/15 10:00  12/30/15 4:00   12/29/15 9:00   12/27/15 1:00
    2273    NA  12/30/15 7:00   12/29/15 6:00   12/27/15 9:00
    2274    12/31/2015  15:00:00    12/30/15 10:00  12/29/15 5:00   12/27/15 7:00

VehicleParking_OutTime

 Vehicle_Id 12/31/15    12/30/15    12/29/15    12/27/15 
2268    12/31/15 11:26  NA  12/29/2015  13:00:00    NA
2269    12/31/2015  13:00:00    12/30/2015  15:00:00    12/29/15 0:00   NA
2270    12/31/15 9:00   12/30/2015  17:00:00    12/29/2015  17:00:00    NA
2271    12/31/2015  23:00:00    12/30/2015  13:00:00    12/29/2015 18:00:00     NA
2272    12/31/15 10:30  12/30/2015  14:00:00    12/29/2015  19:00:00    12/27/2015  13:00:00 

2273    NA  12/30/2015  17:00:00    12/29/2015  16:00:00    12/27/2015 19:00:00 
2274    12/31/2015  17:00:00    NA  12/29/2015 15:00:00     12/27/2015  17:00:00 

Following are my challenges 1- The count of columns are 365 and every column name is date so I need a function which take columns as index to calculate the Time difference 2- Both intime and outtime are in different files

I have created a function which is successfully working for calculating the date difference but when both are in same file with different column names

#Using Chron library
    Diff_Minutes <- function(In_Time,Out_Time){
      if(is.na(In_Time) == TRUE){
        return(NA)
      }else{
        return((as.numeric(chron(dates=str_split_fixed(Out_Time," ",2)[,1],times=str_split_fixed(Out_Time," ",2)[,2],format=c('d-m-y','h:m:s'))) - as.numeric(chron(dates=str_split_fixed(In_Time," ",2)[,1],times=str_split_fixed(In_Time," ",2)[,2],format=c('d-m-y','h:m:s'))))*1440)
      }
    }
A.K. Singh
  • 45
  • 9
  • Use `dput` to present them. At the moment the delimiters that must have been in the original data have been lost. We cannot deduce what you have from the print output. – IRTFM Aug 15 '17 at 23:35
  • sir what is dput, since I am new I am not aware of it, Is it a R library ? – A.K. Singh Aug 15 '17 at 23:39
  • [`?dput`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/dput.html). You may also benefit from skimming through https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, which discusses some use of `dput` and friends from the perspective of making it easier for others to help you. – r2evans Aug 15 '17 at 23:44
  • sir I am already doing study of dput but in my case It might helpful because datacase contains datetimes, – A.K. Singh Aug 15 '17 at 23:52
  • 1
    @A.K.Singh 1) I suggest you look at library `tidyr` or `tidyverse` for a function `gather` - this will take a "wide" format like you have and make it a "long format" with one row per vehicle per date. 2) look at the `join` function in package `dplyr` or `tidyverse` which lets you easily combine data from two different dataframes by a common key like vehicle id – Andrew Lavers Aug 16 '17 at 00:03

0 Answers0