Skip to content

Instantly share code, notes, and snippets.

@briandk
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandk/fae6069d916e693c296e to your computer and use it in GitHub Desktop.
Save briandk/fae6069d916e693c296e to your computer and use it in GitHub Desktop.
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