How to filter dataframe from string variable

I know i can filter a dataframe using this command:

a<-c(1,2,3)
b<-c(2,1,2)
c<-c(3,1,1)
test<-data.frame(a,b,c)
test2<-subset(test,a>1&b>1)

which will return (3,2,1), the thing is that a have the filter conditions in a string:

filt<-"a>1&b>1"

is there a way i can filter using this variable? like subset(test,filt)

i tried subset(test,eval(filt)) but does not work

Upvotes: 0

Views: 37

Answers (1)

YOLO
YOLO

Reputation: 21749

You can do:

subset(test, eval(parse(text = filt)))

Upvotes: 1

Related Questions