My dataframe has 22 variables. this is a simplified sample. the variables include x1,x2,y1_,y2_. i want to create a new variable. the variable values are x1*y1_+x2*y2_
. the code is as follows:
df <- data.frame(x1=c(0,0,0,1),x2=c(0,0,0,1),y1_=c(3,0,2,1),y2_=c(1,0,0,1))
df$var <- df$x1*df$y1_+df$x2*df$y2_
if no. of variables is 22, the above code is unreasonable. so,how to get this variable?