Olivier
Olivier

Reputation: 376

sapply use in conjunction with dplyr's mutate_at

I am trying to clean-up some data stored in multiple data frames using the same function repeatedly. I am trying in this example to leverage mutate_at from dplyr to convert to Date format all columns names which contain 'date'.

I have a list of tables in my environment such as:

table_list <- c('table_1','table_2','table_3')

The objective for me is to overwrite each of the tables for which the name is listed in table_list with their corrected version. Instead I can only get the results stored in a large list.

I have so far created a basic function as follows:

fix_dates   <-  function(df_name){
  get(df_name) %>% 
     mutate_at(vars(contains('date')),
              funs(as.Date(.,
                    origin = "1899-12-30")
              ))
}

The fix_dates() function works perfectly fine if I feed it one element at a time with for example fix_dates('table_1').

However if I use sapply such as results <- sapply(table_list, fix_dates) then I will find in results list all the tables from table_list at their respective indexes. However I would like to instead have table_1 <- fix_dates('table_1') instead for each of the elements of table_list

Is it possible to have sapply store the results in-place instead?

Upvotes: 0

Views: 1012

Answers (1)

ulfelder
ulfelder

Reputation: 5335

There's probably a more elegant way to do this, but I think this gets you where you want to go:

# use lapply to get a list with the transformed versions of the data frames
# named in table_list
new_tables <- lapply(table_list, function(x) {

   mutate_at(get(x), vars(contains("date")), funs(as.Date(., origin = "1899-12-30")))

})

# assign the original df names to the elements of that list
names(new_tables) <- table_list

# use list2env to put those list elements in the current environment, overwriting
# the ones that were there before
list2env(new_tables, envir = environment())

Upvotes: 2

Related Questions