Suppose I have a data frame with some columns containing text and that in some cases there are spaces. In those cases I would like to replace the spaces with underscores.
e.g.
library(dplyr)
library(stringr)
my_mtcars <- mtcars %>% rownames_to_column()
In this case there is only one column, 'rowname' which contains text. But in my real world example my df may have a variable number of fields containing text that I would like to apply this transformation on.
Tried:
my_mtcars %>% mutate_at(any_vars, str_replace_all(' ', '_'))
Error: `.vars` must be a character/numeric vector or a `vars()` object, not a function
Then tried:
my_mtcars %>% mutate_all(str_replace_all(' ', '_'))
Error in fix_replacement(replacement) :
argument "replacement" is missing, with no default
How can I transform any features with text data so that any spaces are replaced with underscores?