I have a dataframe which looks like this
var1<-c(150,20,30)
var2<-c(20,30,40)
x<-c(2.5,4.5,7.5)
s_var1<-c(0,0,0)
s_var2<-c(0,0,0)
data<-data.frame(var1, var2, x, s_var1, s_var2)
There could be a whole bunch of 'var' columns - var1, var2....var'n'. Same with s_var1, s_var2.....etc.
I want to write a function which does calculations on the 's_var' columns while referencing the 'var' columns and the 'x' column.
For example: if there are 2 'var' columns
n_var<-c(1,2)
for (i in n_var)
{
if (x > 2.5) { s_var[i] = var[i] } else {s_var[i] = 2*var[i]}
}
Any suggestions? I am struggling to pass the numbers in the list as suffixes to reference column names...