Search code examples
rtidyversensedplyr

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 run

new(name = "newVar")

it doesn't work. I know mutate_() could help but I'm struggling in using it together with ifelse.

Any help would be appreciated.


Solution

  • Using dplyr 0.7.1 and its advances in NSE, you have to UQ the argument to mutate and then use := when assigning. There is lots of info on programming with dplyr and NSE here: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html

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

    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")
    

    This returns

         x        y 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