'as.POSIXct returning a double when used in a function instead of DateTime
I have a messy database to deal with where the date time was sometimes stored as 24 hour format with no seconds and other times it was stored as 12 hour time format with AM/PM at the end (could have happened during a Windows update of our measurement computer or something, I don't know).
I want to convert the DateTime string to a usable DateTime object with as.POSIXct but when I try the follow code it is converted into a double (checked the class it is also numeric)
main_function <- function(res_df)
{
res_df <- res_df %>%
mutate(DateTime = sapply(DateTime, date_time_convert))
}
date_time_convert <- function(dt_string, tz="Europe/Amsterdam")
{
if(str_detect(dt_string, "M")){
dt_format <- "%m/%d/%Y %I:%M:%S %p"
}else
{
dt_format <- "%m/%d/%Y %H:%M"
}
as.POSIXct(dt_string, format=dt_format, tz=tz)
}
When I debug, the code executes properly in the function (returns a DateTime object), but when it moves into my dataframe the dates are all converted into doubles.
Solution 1:[1]
sapply and similar do not always play well with POSIXt as an output. Here's an alternative: use do.call(c, lapply(..., date_time_convert)).
Demo with sample data:
vec <- c("2021-01-01", "2022-01-01")
### neither 'sapply(..)' nor 'unlist(lapply(..))' work
sapply(vec, as.POSIXct)
# 2021-01-01 2022-01-01
# 1609477200 1641013200
unlist(lapply(vec, as.POSIXct))
# [1] 1609477200 1641013200
do.call(c, lapply(vec, as.POSIXct))
# [1] "2021-01-01 EST" "2022-01-01 EST"
which means your code would be
res_df %>%
mutate(DateTime = do.call(c, lapply(DateTime, date_time_convert)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | r2evans |
