Skip to content

Instantly share code, notes, and snippets.

@briandk
Last active Aug 29, 2015
Embed
What would you like to do?
Using mutate_each with functions that aren't window functions
library(ggplot2)
library(magrittr)
glimpse(mpg) # note that `trans` and `drv`, for example, are factors
coerce_factors_to_characters <- function (x) {
return(
ifelse(
is.factor(x),
as.character(x),
x
)
)
}
# After the piping, `trans` and `drv` are character vectors,
# which is what I wanted.
# Note that my function wasn't, at least I don't think,
# a window function :-). Note also the cheat-sheet *only* says:
#
# > dplyr::mutate_each(iris, funs(min_rank))
# > Apply *window* function to each column.
#
# (my emphasis added)
mpg %>%
mutate_each(funs(coerce_factors_to_characters)) %>%
glimpse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment