I am new to Stackoverflow and quite new to R. I would really appreciate your help.
I am using dplyr
's mutate()
function to create a set new columns based on one initial column. For an a priori known number of columns to be created, everything works fine.
However, in my application, the number of new columns to be created is unknown (or rather determined as input parameter before running the code).
For illustration, consider the following minimal working example:
library(dplyr)
x <- c(1, 2, 3)
df <- data.frame(x)
df <- df %>%
mutate(x1 = x + 1) %>%
mutate(x2 = x + 2) %>%
mutate(x3 = x + 3)
In this example, I create three new variables. However, I want the program to work with a dynamic number of variables (e.g., five or ten new variables).
Some background for my real life application: I want to use the DB2's function ADD_MONTHS(). So I need dplyr
/dbplyr
to flush that function directly into an SQL command. I therefore need a solution that actually does not use data frame logic - I need the solution to be in dplyr
.
From a different perspective: In SAS I'd use the macro processor to dynamically build a proc sql statement. Is there an equivalent in R?