Convert all columns to characters in a data.frame
dataframe, r
Solution
EDIT: 2021-03-01
Beginning with dplyr 1.0.0, the `_all()` function variants are deprecated. The new way to accomplish this is using the new `across()` function.
library(dplyr)
mtcars %>%
mutate(across(everything(), as.character))
With `across()`, we choose the set of columns we want to modify using tidyselect helpers (here we use `everything()` to choose all columns), and then specify the function we want to apply to each of the selected columns. In this case, that is `as.character()`.
Original answer:
You can also use `dplyr::mutate_all`.
library(dplyr)
mtcars %>%
mutate_all(as.character)
Problem
Consider a data.frame with a mix of data types. For a weird purpose, a user needs to convert all columns to characters. How is it best done? A tidyverse attempt at solution is this: ``` map(mtcars,as.character) %>% map_df(as.list) %>% View() c2<-map(mtcars,as.character) %>% map_df(as.list) ``` when I call `str(c2)` it should say a tibble or data.frame with all characters. The other option would be some parameter settings for `write.csv()` or in `write_csv()` to achieve the same thing in the resulting file output.