Search code examples
rdatetimepipelinelubridate

How can I use lubridate parsing function with a R pipeline?


I'm trying to write a pipeline that parses a date vector and subtracts 4 hours from each value. Here's some sample data:

structure(list(Created = c("24/04/2019 05:03:45", "24/04/2019 05:03:47", 
"24/04/2019 05:03:56", "24/04/2019 05:04:00", "24/04/2019 11:51:57", 
"24/04/2019 05:58:21", "23/04/2019 10:36:24", "24/04/2019 01:33:53", 
"23/04/2019 18:44:50", "23/04/2019 18:25:19"), Ended = c("Â", 
"Â", "Â", "Â", "24/04/2019 12:20:26", "24/04/2019 11:51:57", 
"23/04/2019 10:51:21", "24/04/2019 05:03:56", "24/04/2019 01:33:53", 
"23/04/2019 18:44:50")), row.names = c(NA, 10L), class = "data.frame")

This works:

Data$Ended <- dmy_hms(Data$Ended)
Data$Ended <- Data$Ended - hours(4)

But this first step doesn't:

Data$Ended %>% dmy_hms()

I get this Warning message: All formats failed to parse. No formats found.


Solution

  • For such kind of transformation you've better use mutate() And pipeline framework allow you to avoid using Data$ for each field. If we pretend that Data looks like this, you can try:

    Data <- tibble(Ended=c('23-04-2019 00:00:00', '23-04-2019 01:00:00', 
                           '24-04-2019 00:00:00', '24-04-2019 01:00:00'))
    
    Data <- Data %>% 
      mutate(Ended=dmy_hms(Ended)- hours(4))