How to paste text and variables into a logical expression in R?
- by Jasper
I want to paste variables in the logical expression that I am using to subset data, but the subset function does not see them as column names when pasted (either with ot without quotes).
I have a dataframe with columns named col1, col2 etc. I want to subset for the rows in which colx < 0.05
This DOES work:
subsetdata<-subset(dataframe, col1<0.05)
subsetdata<-subset(dataframe, col2<0.05)
This does NOT work:
for (k in 1:2){
subsetdata<-subset(dataframe, paste("col",k,sep="")<0.05)
}
for (k in 1:2){
subsetdata<-subset(dataframe, noquote(paste("col",k,sep=""))<0.05)
}
I can't find the answer; any suggestions?