Things I forget: dynamically assign variable names in dplyr

2019/05/04

Setting variable names dynamically is often a signal of bad programming, and I do it all of the time. Here’s how to do it in dplyr 0.7 and later.

multipetal <- function(df, n) {
    varname <- paste("petal", n , sep=".")
    mutate(df, !!varname := Petal.Width * n)
}

Source: SO post