I'm applying this function to a list in R.
tmp<-lapply(mydata,transform, V3 = ifelse(V2 > 20, V3, NA))
Each element in the list is a dataframe with 3 numeric columns V1, V2 and V3. The code above works just fine.
But if I try to set the columns as variables:
colA<-paste("V",2,sep="")
colB<-paste("V",3,sep="")
and then:
tmp<-lapply(mydata,transform, colB = ifelse(colA > 20, colB, NA))
this doesn't work. It creates a new column named "colB" fill with "V3" string.
I also tried with get:
tmp<-lapply(mydata,transform, get(colB) = ifelse(get(colA) > 20, get(colB), NA))
Error: unexpected '=' in "tmp<-lapply(mydata,transform, get(colB) ="
Is there any way to pass a variable with the column name in R? The final aim is that colA and colB are passed as command line arguments when calling the script with Rscript because the same code could be applied to different lists with variable number of columns. Thanks