0

I'm trying to use filter function in dplyr package to extract rows from a dataframe that fits the condition.

My dataframe looks like this:

head(EIA)
  length location  stype  rock  dist  ftype  depth aspect  degree  drain  TWI
1   len1     loc3 stype2 rock3 dist3 ftype1 depth2   asp2 degree2 drain4 TWI1
2   len4     loc2 stype2 rock3 dist1 ftype4 depth3   asp4 degree3 drain4 TWI3
3   len2     loc2 stype2 rock2 dist1 ftype4 depth3   asp1 degree2 drain4 TWI2
4   len4     loc2 stype2 rock3 dist2 ftype4 depth3   asp4 degree2 drain4 TWI2
5   len4     loc2 stype1 rock3 dist1 ftype2 depth3   asp1 degree3 drain4 TWI2
6   len4     loc3 stype2 rock2 dist1 ftype2 depth3   asp4 degree3 drain1 TWI2

and so on. Total rows: 10560

But since I'm trying to do this job in a for loop, each elements in filter function has to be passed with variables(colcol, aa, bb, cc):

 colcol
    [1] "length"   "location" "stype"    
 aa;bb;cc
    [1] "len1"
    [1] "loc2"
    [1] "stype1"

I tried to do the filter function like this but didn't work:

filter(EIA, colcol[1] == aa & colcol[2] == bb & colcol[3] == cc)

Two rows must be extracted as a result. I think this is because of the double quotes around colcol[1] element. I tried to remove these quotes by using as.symbol, as.name, and noquote functions but they didn't work.

Is there any solution to this problem?

Thank you in advance.

SJL
  • 3
  • 3

2 Answers2

1

subset is best solution in such kind of problem

 aa <- subset(EIA,EIA$lenght=='len1')

above example you might like and simple and smart solution. you can use and (&) condition also to extract rows.

 aa <- subset(EIA,EIA$lenght=='len1' & EIA$location=='loc2')
Tushar Lad
  • 490
  • 1
  • 4
  • 17
0

Convert it into a symbol and then evaluate

library(dplyr)
filter(EIA,!!sym(colcol[1]) == aa & !!sym(colcol[2]) == bb & !!sym(colcol[3]) == cc)

For example when we evaluate first conditon, we get

filter(EIA, !!sym(colcol[1]) == aa)
#  length location  stype  rock  dist  ftype  depth aspect  degree  drain  TWI
#1   len1     loc3 stype2 rock3 dist3 ftype1 depth2   asp2 degree2 drain4 TWI1

data

EIA <- structure(list(length = structure(c(1L, 3L, 2L, 3L, 3L, 3L), 
.Label = c("len1", "len2", "len4"), class = "factor"), location = structure(c(2L, 
1L, 1L, 1L, 1L, 2L), .Label = c("loc2", "loc3"), class = "factor"), 
stype = structure(c(2L, 2L, 2L, 2L, 1L, 2L), .Label = c("stype1", 
"stype2"), class = "factor"), rock = structure(c(2L, 2L, 
1L, 2L, 2L, 1L), .Label = c("rock2", "rock3"), class = "factor"), 
dist = structure(c(3L, 1L, 1L, 2L, 1L, 1L), .Label = c("dist1", 
"dist2", "dist3"), class = "factor"), ftype = structure(c(1L, 
3L, 3L, 3L, 2L, 2L), .Label = c("ftype1", "ftype2", "ftype4"
), class = "factor"), depth = structure(c(1L, 2L, 2L, 2L, 
2L, 2L), .Label = c("depth2", "depth3"), class = "factor"), 
aspect = structure(c(2L, 3L, 1L, 3L, 1L, 3L), .Label = c("asp1", 
"asp2", "asp4"), class = "factor"), degree = structure(c(1L, 
2L, 1L, 1L, 2L, 2L), .Label = c("degree2", "degree3"), class = "factor"), 
drain = structure(c(2L, 2L, 2L, 2L, 2L, 1L), .Label = c("drain1", 
"drain4"), class = "factor"), TWI = structure(c(1L, 3L, 2L, 
2L, 2L, 2L), .Label = c("TWI1", "TWI2", "TWI3"), class = "factor")), 
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))

colcol <- c("length",   "location", "dist")
aa <- "len1"
bb <- "loc2"
cc <- "stype1"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213