0

I have a list of dataframes with common columns that I need to merge, but I need to coerce certain columns to character type first prior to merging. To fix this, I wrote the following:

master_list <- lapply(master_list, function(x) x %>%
                     mutate_at(.vars = vars(master_list$x$'send date',
                                            master_list$x$'send time',
                                            master_list$x$'Monthly',
                                            master_list$x$'InteractionEventDate'),
                               .funs = as.character))

However, when I look at these columns in any one dataframe in my list, the change was not made (e.g. InteractionEventDate is still a double), despite no error being thrown by the above line of code. I based it partially on the answer to this post, which used the now deprecated mutate_each.

The_Dude
  • 39
  • 4

1 Answers1

0

Your approach is a bit roundabout here. So maybe first define a function using dplyr logic and then lapply it over the list?

premerge <- function(df){
            df %>%
               mutate_at(.vars = c("send date","send time", "Monthly","InteractionEventDate"), .funs = as.character)                        
              }

lappyl(master_list,premerge)
Fnguyen
  • 1,159
  • 10
  • 23