Deepak Yadav - 1 year ago 63
R Question

# Store data frame in the form of query

`data frame`
`D`
is
`fixed`
.

``````> D
A1 A2 A3 A4 A5
1   1  1  1  1  1
2   1  1  2  1  3
3   1  2  1  2  2
4   1  1  2  2  2
5   1  2  2  3  2
6   1  2  2  3  2
7   2  2  1  4  1
8   2  2  1  4  1
9   2  3  1  5  1
10  2  3  2  6  1
``````

``````> D[D\$A1==1 & D\$A2 == 1 & D\$A4 !=1 , c("A2", "A3", "A4")]
A2 A3 A4
4  1  2  2
``````

The above query results in a data frame. But instead of storing resulting data frame I want to store the query which results in the above data frame. I want to store the query in such a way so that later on query can be executed easily.

One method is to store the query in the form of string. And later on if execution is required, first we will parse and then execute the query.

Other operations required for stored query

1. add condition of the form
`& D\$A5==3`

3. delete column

Suppose initial query was
`D[D\$A1==1 & D\$A2 == 1 & D\$A4 !=1 , c("A2", "A3", "A4")]`

`D\$A5==3`
to the query

i.e.
`D[D\$A1==1 & D\$A2 == 1 & D\$A4 !=1 & D\$A5==3 , c("A2", "A3", "A4")`

`"A5"`

i.e.
`D[D\$A1==1 & D\$A2 == 1 & D\$A4 !=1 & D\$A5==3 , c("A2", "A3", "A4", "A5")`

Delete column "A2"

i.e.
`D[D\$A1==1 & D\$A2 == 1 & D\$A4 !=1 & D\$A5==3 , c("A3", "A4", "A5")`

Is there any better and efficient way to do it?

You can build it into a function call:

``````   a12.234 <-function(dat, sub = list(NULL), cols= c("A2", "A3", "A4") )
subset(dat,
subset = eval(bquote( A1==1 & A2 == 1 & .( if (
is.null(sub[[1]]) ){TRUE}else{sub} )) ),
select= cols)
> a12.234(D)
A2 A3 A4
1  1  1  1
2  1  2  1
4  1  2  2
> a12.234(D, sub=quote(A5 == 3) )
A2 A3 A4
2  1  2  1
``````

``````> a12.234 <-function(dat, sub = list(NULL), add = "")
subset(dat, eval(bquote( A1==1 & A2 == 1 & .( if (
is.null(sub[[1]]) ){TRUE}else{sub} )) ),
> a12.234(D, sub=quote(A5 == 3) )
A2 A3 A4
2  1  2  1
> a12.234(D, sub=quote(A5 == 3), add="A1" )
A2 A3 A4 A1
2  1  2  1  1
``````

And the final request: deletion from the base vector of column names:

``````a12.234 <-function(dat, sub = list(NULL), add = "", delete=NULL)
subset(dat, eval(bquote( A1==1 & A2 == 1 &
.( if ( is.null(sub[[1]]) ){TRUE}else{
sub} )) ),