Search code examples
rdplyrtidyversetidyrlubridate

Converting multiple columns from character to POSIXct in R


I have multiple dataframes with multiple date/time columns that are initially character variables when I import them and I would like to convert the to a POSIXct format. I am open to other date/time formats as well.

Here is a sample out one of my dataframes:

visit<-as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
                      ICU_FIRST=structure(c("03-08-2018 12:00:00", "03-17-2018 05:32:00", "02-11-2018 09:15:00", "04-01-2019 15:10:00", 
                                            "04-30-2018 06:05:00", "04-18-2018 18:45:00")),
                      ICU_LAST=structure(c("03-10-2018 10:27:00", "03-17-2018 15:45:00", "02-27-2018 09:30:00", "04-05-2019 18:49:00", 
                                           "05-29-2018 06:48:00", "04-19-2018 19:33:00")))))

I tried using the mutate(across(starts_with())) option combined with as.POSIXct:

visit<-visit%>%
  mutate(across(as.POSIXct(starts_with("ICU"), format="%m-%d-%Y %H:%M")))

but I keep getting the error:

Problem while evaluating as.POSIXct(starts_with(x), format = "%m-%d-%Y %H:%M").

I also tried this:

visit<-visit%>%
  mutate(across(starts_with("ICU"), as.POSIXct))

but I got the message character string is not in a standard unambiguous format, which to my understanding is the result of the two ICU columns not being numeric.

I ideally would love to insert this code into a global function, but I am trying to get it to work for this dataframe first.


Solution

  • A few pieces missing or out of order. I added %S and corrected order of functions. (Actually %S isn't necessary.)

    library(dplyr, warn.conflicts = FALSE)
    
    visit <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
                                          ICU_FIRST=structure(c("03-08-2018 12:00:00", 
                                                                "03-17-2018 05:32:00", 
                                                                "02-11-2018 09:15:00", 
                                                                "04-01-2019 15:10:00", 
                                                                "04-30-2018 06:05:00", 
                                                                "04-18-2018 18:45:00")),
                                        ICU_LAST=structure(c("03-10-2018 10:27:00", 
                                                             "03-17-2018 15:45:00",
                                                             "02-27-2018 09:30:00", 
                                                             "04-05-2019 18:49:00", 
                                                             "05-29-2018 06:48:00",
                                                             "04-19-2018 19:33:00")))))
    
    visit <- 
      visit |>
      mutate(across(starts_with("ICU"), 
                    \(x) as.POSIXct(x, format="%m-%d-%Y %H:%M:%S")))
    visit
    #>   EncounterID           ICU_FIRST            ICU_LAST
    #> 1           1 2018-03-08 12:00:00 2018-03-10 10:27:00
    #> 2           2 2018-03-17 05:32:00 2018-03-17 15:45:00
    #> 3           3 2018-02-11 09:15:00 2018-02-27 09:30:00
    #> 4           4 2019-04-01 15:10:00 2019-04-05 18:49:00
    #> 5           5 2018-04-30 06:05:00 2018-05-29 06:48:00
    #> 6           6 2018-04-18 18:45:00 2018-04-19 19:33:00
    

    Created on 2024-01-22 with reprex v2.0.2