Hello! I am trying to bring date and time data into RStudio and create a datetime column. I seem to be having trouble when I try and use lubridate functions; I receive warning messages that all formats failed to parse. The datetime column is created but all values are "na". Even under "#Parse Date column", I had tried to use mdy() opposed to as.Date(), but mdy() would not work. Please see reprex below. Thank you!
library("tidyverse")
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
#Create sample dataset
MV.71 <- data.frame(stringsAsFactors = FALSE,
DATE = c(NA, NA, "10/15/2020", "10/15/2020", "10/15/2020",
"10/15/2020"),
TIME = structure(c(NA, NA, 48600, 50400, 52200, 54000),
class = c("hms", "difftime"), units = "secs"))
#Parse DATE column
MV.71 <- MV.71 %>%
mutate(DATE = as.Date(DATE,
tz = "America/New_York",
format = "%m/%d/%Y"))
#Create datetime_string as character from DATE and TIME
#Create datetime from datetime_string
MV.71 <- MV.71 %>%
mutate(
datetime_string = paste(DATE, TIME, sep = "_"),
datetime = lubridate::mdy_hms(datetime_string,
tz = "America/New_York"))
#> Warning: Problem with `mutate()` input `datetime`.
#> i All formats failed to parse. No formats found.
#> i Input `datetime` is `lubridate::mdy_hms(datetime_string, tz = "America/New_York")`.
#> Warning: All formats failed to parse. No formats found.
Thank you so much! I tried this and it worked for me. I had tried this before since the console shows the DATE column in ymd, despite having set it to mdy with as.DATE() but I guess the ones that don't parse are just the "na" values in my spreadsheet. Thank you again, I really appreciate your help!
Thank you so much for helping me with this! make_datetime() will definitely be useful. I also really like how you set the warn.conflicts argument to false, this is a fantastic tip that I will use in the future! Thanks again!