Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created February 17, 2019 04:07
Show Gist options
  • Save yutannihilation/6ff6f207c016d4557f20101bb6db8711 to your computer and use it in GitHub Desktop.
Save yutannihilation/6ff6f207c016d4557f20101bb6db8711 to your computer and use it in GitHub Desktop.
library(dplyr, warn.conflicts = FALSE)
library(purrr)

quo_tmpl <- quo(as.character(.))

quos <- iris %>%
  map_lgl(is.factor) %>%
  { syms(set_names(names(.)[.])) } %>%
  map(dplyr:::expr_substitute, expr = quo_tmpl, old = quote(.))

dplyr::mutate(iris, !!!quos) %>%
  as_tibble()
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <chr>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ... with 140 more rows

Created on 2019-02-17 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment