# Some data
df <- structure(list(session_id = c("1605859261112.d502cbs9", "1605859261112.d502cbs9",
"1605859261112.d502cbs9", "1605859261112.d502cbs9", "1605859283839.hjadh9ah",
"1605859471370.p1dd4rol"), timestamp = c("1605859226452", "1605859226461",
"1605859248803", "1605859261112", "1605859283839", "1605859471370"
)), row.names = c(NA, 6L), class = "data.frame")
mydfs <- list(
df1 = df,
df2 = df
)
# my custom function
strtime_to_dt <- function(x) {
format(as.POSIXct(as.numeric(x) / 1000, origin = "1970-01-01", tz = "UTC"), "%Y-%m-%d %H:%M:%OS3")
}
I would like to apply my custom function to each df in mydfs. I'd like to mutate column timestamp to be strtime_to_dt(timestamp) Tried:
mydfs <- lapply(mydfs, function(x) mutate(x, timestamp = strtime_to_dt(x$timestamp)))
Error: Problem with `mutate()` input `timestamp`.
x Input `timestamp` can't be recycled to size 132.
ℹ Input `timestamp` is `strtime_to_dt(x$timestamp)`.
ℹ Input `timestamp` must be size 132 or 1, not 0.
Then tried:
mydfs <- mydfs %>% map(~mutate(., timestamp = strtime_to_dt(.$timestamp)))
Error: Problem with `mutate()` input `timestamp`.
x Input `timestamp` can't be recycled to size 132.
ℹ Input `timestamp` is `strtime_to_dt(.$timestamp)`.
ℹ Input `timestamp` must be size 132 or 1, not 0.
How can I apply my function strtime_to_dt
to each dataframe in mydfs?