0

I have a list of dataframes - some dataframes in this list require their columns to mutated into date columns. I was wondering if it possible to do this with mapply.

Here is my attempt (files1 is the list of dataframes, c("data, data1") are the names of dataframes within files1, c("adfFlowDate","datedate") are the names of the columns within the respective dataframes:

files2 <- repair_dates(files1, c("data, data1"), c("adfFlowDate","datedate"))

The function that does not work:

repair_dates <- function(data, df_list, col_list) {
mapply(function(n, i) data[[n]] <<- data[[n]] %>% mutate(i = as.Date(i, origin = "1970-01-01")), df_list, col_list)
return(data)
}
MGJ-123
  • 614
  • 4
  • 19
  • What is `y` in `as.Date(y, origin =` ? – Allan Cameron Jun 11 '20 at 14:38
  • apologies that was mean to be i – MGJ-123 Jun 11 '20 at 14:41
  • 1
    Hi MGJ, it will be much easier to help if you provide sample data with `dput()` and expected output. Maybe you could try `dput(lapply(files1,head))`. You can [edit] your question and paste the output. You can surround it with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jun 11 '20 at 14:55

1 Answers1

2

Your set-up is fairly complex here, calling an anonymous function inside an mapply inside another function, which takes three parameters, all relating to a single nested object.

Personally, I wouldn't add to this complexity by accommodating the non-standard evaluation required to get mutate to work here (though it is possible). Perhaps something like this (though difficult to tell without any reproducible data) -

repair_dates <- function(data, df_list, col_list) 
{
  mapply(function(n, i) {
           data[[n]][[i]] <- as.Date(data[[n]][[i]], origin = "1970-01-01")
           return(data[[n]])
         }, df_list, col_list, SIMPLIFY = FALSE)
}
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87