I have a dataframe, df
, with several columns in it. I would like to create a function to create new columns dynamically using existing column names. Part of it is using the last four characters of an existing column name. For example, I would like to create a variable names df$rev_2002
like so:
df$rev_2002 <- df$avg_2002 * df$quantity
The problem is I would like to be able to run the function every time a new column (say, df$avg_2003
) is appended to the dataframe.
To this end, I used the following function to extract the last 4 characters of the df$avg_2002
variable:
substRight <- function (x,n) {
substr(x, nchar(x)-n+1, nchar(x))
}
I tried putting together another function to create the columns:
revved <- function(x, y, z){
z = x * y
names(z) <- paste('revenue', substRight(x,4), sep = "_")
return x
}
But when I try it on actual data I don't get new columns in my df
. The desired result is a series of variables in my df
such as:
df$rev_2002
, df$rev_2003
...df$rev_2020
or whatever is the largest value of the last four characters of the x
variable (df$avg_2002
in example above).
Any help or advice would be truly appreciated. I'm really in the woods here.