Apply mutate across a list of data frames

# 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?

> lapply(dat[,2], strtime_to_dt) %>% unlist()
[1] "2020-11-20 08:00:26.451" "2020-11-20 08:00:26.460"
[3] "2020-11-20 08:00:48.802" "2020-11-20 08:01:01.111"
[5] "2020-11-20 08:01:23.838" "2020-11-20 08:04:31.369"

(I avoid naming objects after built-ins, so dat = your df. In some operations, the namespace gods treat a data frame named df as a closure because it thinks it is the function named df. Same with data.

Sorry. Object slipped out of my hands:

suppressPackageStartupMessages({
  library(dplyr)
})
# 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")
}

lapply(mydfs[[1]][2], strtime_to_dt) %>% unlist()
#>                timestamp1                timestamp2                timestamp3 
#> "2020-11-20 08:00:26.451" "2020-11-20 08:00:26.460" "2020-11-20 08:00:48.802" 
#>                timestamp4                timestamp5                timestamp6 
#> "2020-11-20 08:01:01.111" "2020-11-20 08:01:23.838" "2020-11-20 08:04:31.369"

Created on 2020-12-09 by the reprex package (v0.3.0.9001)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hi tried this and got:

 lapply(mydfs[,2], strtime_to_dt) %>% unlist()
Error in mydfs[, 2] : incorrect number of dimensions