Deepak Yadav Deepak Yadav - 1 month ago 13
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

  2. add column

  3. delete column



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


Add
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")


Add column
"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?

42- 42-
Answer

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

Adding columns:

> a12.234 <-function(dat, sub = list(NULL), add = "") 
      subset(dat, eval(bquote( A1==1 & A2 == 1 & .( if ( 
                                          is.null(sub[[1]]) ){TRUE}else{sub} )) ), 
                 select= c("A2", "A3", "A4", if(add==""){NULL}else{add} ))
> 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} )) ), 
               select= c("A2", "A3", "A4", if(add==""){NULL}else{add} )[
                  if(delete != ""){ -which( c("A2", "A3", "A4") %in% delete)}else{
                                    TRUE}] )
> a12.234( D, delete="A2")
  A3 A4
1  1  1
2  2  1
4  2  2

I admit this seems a bit tortured.

Comments