Use dplyr mutate () in programming

I am trying to assign a column name to a variable using mutate.

df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } 

When i started

 new(name = "newVar") 

he does not work. I know mutate_() can help, but I'm afraid to use it with ifelse .

Any help would be appreciated.

+5
source share
3 answers

Using dplyr 0.7.1 and its achievements in NSE, you must mutate UQ argument, and then use := when assigning. There is a lot of programming information with dplyr and NSE here: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html

I changed the name of the function argument to myvar to avoid confusion. You can also use case_when from dplyr instead of ifelse if you have more categories to transcode.

 df <- data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(myvar){ df %>% mutate(UQ(myvar) := ifelse(x < 50, "small", "big")) } new(myvar = "newVar") 

It returns

  xy newVar 1 37 1.82669 small 2 63 -0.04333 big 3 46 0.20748 small 4 93 0.94169 big 5 83 -0.15678 big 6 14 -1.43567 small 7 61 0.35173 big 8 26 -0.71826 small 9 21 1.09237 small 10 90 1.99185 big 11 60 -1.01408 big 12 70 0.87534 big 13 55 0.85325 big 14 38 1.70972 small 15 6 0.74836 small 16 23 -0.08528 small 17 27 2.02613 small 18 76 -0.45648 big 19 97 1.20124 big 20 99 -0.34930 big 21 74 1.77341 big 22 72 -0.32862 big 23 64 -0.07994 big 24 53 -0.40116 big 25 16 -0.70226 small 26 8 0.78965 small 27 34 0.01871 small 28 24 1.95154 small 29 82 -0.70616 big 30 77 -0.40387 big 31 43 -0.88383 small 32 88 -0.21862 big 33 45 0.53409 small 34 29 -2.29234 small 35 54 1.00730 big 36 22 -0.62636 small 37 100 0.75193 big 38 52 -0.41389 big 39 36 0.19817 small 40 89 -0.49224 big 41 81 -1.51998 big 42 18 0.57047 small 43 78 -0.44445 big 44 49 -0.08845 small 45 20 0.14014 small 46 32 0.48094 small 47 1 -0.12224 small 48 66 0.48769 big 49 11 -0.49005 small 50 87 -0.25517 big 
+9
source

Following the dlyr program vignette , define your function as follows:

 new <- function(name) { nn <- enquo(name) %>% quo_name() df %>% mutate( !!nn := ifelse(x <50, "small", "big")) } 

enquo takes an argument of the expression and quotes it, then quo_name converts it to a string. Since nn now quoted, we need to say mutate so that it does not quote it a second time. This is for !! . Finally := is a helper operator to make it a valid R-code. Note that with this definition, you can simply pass newVar instead of "newVar" into your function, supporting the dplyr style.

 > new( newVar ) %>% head xy newVar 1 94 -1.07642088 big 2 85 0.68746266 big 3 80 0.02630903 big 4 74 0.18323506 big 5 86 0.85086915 big 6 38 0.41882858 small 
+6
source

Base R Solution

 df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df[,name]='s' df[,name][df$x>50]='b' return(df) } 

I use dplyr 0.5 , so just combine the R base with mutate

 new <- function(Name){ df=mutate(df,ifelse(x <50, "small", "big")) names(df)[3]=Name return(df) } new("newVar") 
+1
source

Source: https://habr.com/ru/post/1269946/


All Articles