how can I get R to read a string as a restriction?

55 Views Asked by At

Instead of writing for a vector V:

V[a>1 & b==2 & c<1]

I would like something like:

V[restriction]

with

restriction = "a>1 & b==2 & c<1"

Any ideas? Thanks, F.

1

There are 1 best solutions below

0
On BEST ANSWER

Try this:

> V <- data.frame(a = 1:5, b = 1:5, c = 0)
> restriction = "a>1 & b==2 & c<1"
> subset(V, eval(parse(text = restriction)))
  a b c
2 2 2 0