I've looked at a couple of previous examples with mutate
, across
, and map
but struggled to fully understand them. Apologies if this question is a duplicate. Here are the other two posts that may be relevant - Using mutate(across(...)) with purrr::map and
purrr::pmap with dplyr::mutate.
Background:
I have a list of ten dataframes. All of them have a similar number of column and names. (Some may have one or two more.) My goal is to combine all the columns into one dataframe, and I plan to use bind_rows()
or list_rbind()
.
Problem:
Because of the poor quality of the raw CSV data files, the same column in different files may be of a different class. As such, running bind_rows()
returns this error.
Error in `bind_rows()`:
! Can't combine `..1$cfv` <character> and `..2$cfv` <double>.
Backtrace:
1. data_list %>% bind_rows()
2. dplyr::bind_rows(.)
Attempted solution:
Because I don't know for sure the class of each column and some dataframes may be missing a column, my thought to overcoming this problem is to first converting all columns to the character class, binding them together, and then converting the relevant columns back to numeric.
To convert all columns of all dataframes in the list to the character class, I thought to use mutate
, across
, and map
.
This is the code.
data = data_list %>%
map(mutate(across(everything(), ~ as.character(.))))
However, it does not work and returns this error message.
Error in `across()`:
! Must only be used inside data-masking verbs like `mutate()`, `filter()`, and `map()`.
Backtrace:
1. data_list %>% ...
6. dplyr::across(everything(), ~as.character(.))
Question:
How do I use mutate()
, across()
, and map()
together? Alternatively, better ways to combine the different dataframes in the list would be welcome, too.
Thanks in advance.