Question:I want to store a column name in a variable and operate a dataframe based on that column name. For example if a I have two columns named
airplane_sales. I have a variable
varthat a user sets to say
car_sales. i then calculate a new column like so:
calc_col <- paste0(var,"_delta") df$calc_col <- abs(df$var - lag(df$var ,12)) [/code]The
varwill change based on user input, so the resulting column will also change
How do I do this in R?
Answer:You could use:
df[[calc_col]] <- abs(df[[var]] - lag(df[[var]], 12)) [/code]
If you have better answer, please add a comment about this, thank you!