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?
Here are a couple of options that are closer to the Jasper’s approach. First, you could define the column name as a separate variable and then use it to select the variable from the
data.frameas if it were alist(since adata.frameis basically alist):Or you could refer to the column name as such:
Finally, you could use
subset, although you need to provide a logical expression (as pointed out by Joshua Ulrich):