I have a list of variable names and I want to use the strings from the list to access columns in data frames
list<-list("Var1", "Var2", "Var3")
df1 <- data.frame("Var1" = 1:2, "Var2" = c(21,15), "Var3" = c(10,9))
df2<- data.frame("Var1" = 1, "Var2" = 16, "Var3" = 8)
I have 2 uneven data frames and I want to create a new column by doing some basic maths
df1$Var4<-df1$Var1 + df2$Var1
Var4
2
3
But i want to be able to call the column names i'm adding together by refering to the list of variables names I have, I have tried the two following pieces of code by neither worked
df1$Var4<- df1$List[1]+df1$list[1]
and
Z<-list[1]
df1$Var4 <- df1$Z + df2$Z
I don't want to hard code the column names because this will be creating a function to be used across data frames where the variable names will change
Any help would be much appreciated