Hi I’m working with a large data frame that I frequently need to subset in different combinations of variables. I’d like to be able to store the search in a string so I can just refer to the string when I want to see a subset.
x = read.table(textConnection("
cat1 cat2 value
A Z 1
A Y 2
A X 3
B N 2"),header=T,strip.white=T)
search_string="cat1== 'A' & cat2=='Z'"
with(x,subset(x,search))
doesn’t work. What I’d be looking for is the result of a search similar to the one below.
with(x,subset(x,cat1=='A' & cat2=='Z'))
I’d prefer not to just create multiple subsetted data frames at the start if another solution exists.
Is there a simple way to do what I’m trying?
You need to convert your string to an expression, and then evaluate this expression
Always remembering
And noting in this case, it is far more typing to go the eval(parse(text==)))
You could also try using
quoteto save thecalland then just use
It is also important to remember that that
subsethas nonstandard evaluation so it might be safer to use`[`or