Map over list of dataframes

Viewed 144

I am trying to map over a list of data frames but I am doing something wrong. I have multiple dataframes in my environment, my objective is to replace all empty strings with NA values what I am doing is the following:

# create a list of my dataframes
lst <- ls()
lst<- lst[sapply(lst, function(x) is.data.frame(get(x)))]
# second, map my function on all of my dataframes
map(lst, ~.x %>% mutate_if(is.character, list(~na_if(.,"")))) 

and I get the following error mesage Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "character"

I think I am doing something wrong with the indexing but I am not sure what it is.

If anyone could help (the output should be data frames) I would be extremely grateful

1 Answers

The issue is that we are not getting the value of the 'lst' after subsetting

library(purrr)
library(dplyr)
map(mget(lst) ~.x %>%
                  mutate_if(is.character, list(~na_if(.,"")))) 
Related