How to paste text and variables into a logical expression in R?
Posted
by
Jasper
on Stack Overflow
See other posts from Stack Overflow
or by Jasper
Published on 2011-06-25T00:08:30Z
Indexed on
2011/06/25
8:22 UTC
Read the original article
Hit count: 216
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?
© Stack Overflow or respective owner