Using mutate_each with functions that aren't window functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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