-2

i'm a beginner in R. I bet this question sounds stupid but its so specific i cannot find information in the internet. I have to calculate process times from a data frame. The problem is, the dataframe has a col of dates and a col of times. Is there an easy possibility for me to merge them to get a time stamp without using loops?

dww
  • 28,320
  • 5
  • 57
  • 99
Ernsthaft
  • 167
  • 1
  • 1
  • 8

1 Answers1

1

Consider this data example:

df <- data.frame(day = as.Date(c("2018-07-25", "2018-07-24")),
                 times = c("05:24:36", "11:27:14"))

         day    times
1 2018-07-25 05:24:36
2 2018-07-24 11:27:14

Now use paste in combination with as.POSIXct to create a timestamp variable with a vectorized operation.

df$ts <- as.POSIXct(paste(df$day, df$times, sep = " "))

         day    times                  ts
1 2018-07-25 05:24:36 2018-07-25 05:24:36
2 2018-07-24 11:27:14 2018-07-24 11:27:14
LAP
  • 6,475
  • 2
  • 13
  • 27