0

Is it possible to update the origin date of a time, based on the date in another column? I have 2 columns, one with 10 rows of Dates, and the other with 10 rows of Times, which have a date attached to them. I originally converted the time from character using as.POSIX1t, and tried setting origin=df$Date, but that did not work. The date just showed up as today's date. How can I reset the origin date of the Time to be the same date as my Date column? Or, is it possible to just remove the Date that is attached to my time, and then create a new column that attaches the Time and Date columns? My data:

       Date                Time
 1999-05-10 2017-10-25 07:50:00
 1999-05-11 2017-10-25 15:30:00
 1999-05-12 2017-10-25 18:00:00
 1999-05-13 2017-10-25 23:05:00
 1999-05-14 2017-10-25 19:30:00
 1999-05-15 2017-10-25 19:00:00
 1999-05-16 2017-10-25 16:10:00
 1999-05-17 2017-10-25 10:00:00
 1999-05-18 2017-10-25 02:30:00
 1999-05-19 2017-10-25 22:00:00
 1999-05-20 2017-10-25 21:50:00

And an str() of the data:

'data.frame':   11 obs. of  2 variables:
 $ Date: Date, format: "1999-05-10" "1999-05-11" "1999-05-12" "1999-05-13" ...
 $ Time: POSIXlt, format: "2017-10-25 07:50:00" "2017-10-25 15:30:00" "2017-10-25 18:00:00" "2017-10-25 23:05:00" ...
coderX
  • 424
  • 5
  • 16
  • [Would help to see](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) some example data. – neilfws Oct 25 '17 at 02:38
  • Sorry, I figured the way I explained it would be good enough. I'll edit with data – coderX Oct 25 '17 at 15:26

1 Answers1

1

assuming that your origin date and time is a character datatype:

df1 <- data.frame(date=c("2017-01-05"), time=c("2017-05-05 10:05:06"), stringsAsFactors = F)
timing <- strsplit(df1$time, split=" ")
timing <- sapply(timing, '[[', 2)
df1$time <- timing
df1$datetime <- paste(df1$date, df1$time)
df1$datetime <- as.POSIXct(df1$datetime)

Would need to see your actual data for more accurate answer.

addicted
  • 2,901
  • 3
  • 28
  • 49
  • Pasted my data. Regardless, your solution worked. Ignore my str() above, that's after I converted my time into date. So keeping time as character, your method was great. Thanks! – coderX Oct 25 '17 at 15:44
  • glad I can help :) – addicted Oct 26 '17 at 02:16