I have a data frame:
a <- c(1,2,3)
b <- c(4,5,6)
c <- c(7,8,9)
d <- c(10,11,12)
e <- data.frame(a=a,b=b,c=c,d=d)
#e
# a b c d
#1 1 4 7 10
#2 2 5 8 11
#3 3 6 9 12
I would like to specify a list of conditions to filter the data frame on (I realize that the example is silly, but bear with me).
I would like to create a function where I can enter a varying number of conditions and get as output the rows that the condition applies to.
So for example, if I enter a list:
condition <- list('a'=3,'b'=6)
Then that would mean that I want:
e[e[,'a'] == 3 & e[,'b'] == 6,]
# a b c d
#3 3 6 9 12
I tried to enter this into a string
st <- "e[,'a'] == 3 & e[,'b'] == 6"
e[get(st),]
This produces an error.
I would imagine that something could be generated using an apply function but I really have no idea how to do it. Any hint would be appreciated.