Function containing dplyr::filter() using column names not working

Hi there,

I am trying to filter out several variables and instead of copying and pasting a bunch, I want to create a function. Here's the function I created:

feat <- function(x){
  v %>% filter(x=="Y")
}

However, whenever I enter in the column name for my data, like so:

veg <- feat(VEGETATION)

I get the following error:

 Error: Problem with `filter()` input `..1`.
i Input `..1` is `x == "Y"`.
x object 'VEGETATION' not found

I'm assuming there must be some sort of syntax issue, but I can't figure out what it is. Googled the problem and not finding any answers. Any help would be greatly appreciated.

Here's a subset of my data:

v <- structure(list(SITE_ID = c("GBA20-10501", "GBA20-10506", "GBA20-10507", 
"GBA20-10508", "GBA20-10509", "GBA20-10510"), VISIT_NO = c(1L, 
1L, 1L, 1L, 1L, 1L), FILENAME = c("GBS20-10501_V1_20210613_A_01", 
"GBS20-10506_V1_20210608_B_01", "GBS20-10507_V1_20210606_A_01", 
"GBS20-10508_V1_20210606_B_01", "GBS20-10509_V1_20210613_B_01", 
"GBS20-10510_V1_20210612_A_01"), VIDEO_QUALITY = c("GOOD", "GOOD", 
"GOOD", "GOOD", "GOOD", "GOOD"), VEGETATION = c("N", "Y", "N", 
"Y", "N", "Y"), MUSSELS = c("N", "Y", "Y", "Y", "Y", "Y"), MUSSEL_ABUNDANCE = c("N", 
"Y", "N", "N", "N", "Y"), DOM_SUBSTRATE = c("S", "S", "S", "H", 
"S", "H"), HUMAN_FEATURES = c("N", "N", "N", "N", "N", "N"), 
    FISH = c("N", "N", "N", "N", "N", "Y"), GOBIES = c("N", "N", 
    "N", "N", "N", "Y"), OTHER_FISH = c("N", "N", "N", "N", "N", 
    "N"), NOTES2 = c("Interesting holes in the sediment", "Hard to see with plume of sediment", 
    "", "Tiny clump of what looks like mussels, could be rocks? Need second opinion", 
    "", "4-5 gobies"), COCR_COMMENTS = c(NA, NA, NA, NA, NA, 
    NA)), row.names = c(NA, 6L), class = "data.frame")

Thanks so much!

The problem is due to the non-standard evaluation used in dpyr functions. Within filter() you write VEGETATION without quotes as if it is a variable name but it is actually the name of a column in v.

filter(VEGETATION == "Y")

You can write your function using the {{ }} operator from rlang.

library(dplyr)
library(rlang)
v <- structure(list(SITE_ID = c("GBA20-10501", "GBA20-10506", "GBA20-10507", 
                                "GBA20-10508", "GBA20-10509", "GBA20-10510"), 
                    VISIT_NO = c(1L, 1L, 1L, 1L, 1L, 1L), 
                    FILENAME = c("GBS20-10501_V1_20210613_A_01","GBS20-10506_V1_20210608_B_01", "GBS20-10507_V1_20210606_A_01", 
                                 "GBS20-10508_V1_20210606_B_01", "GBS20-10509_V1_20210613_B_01", 
                                 "GBS20-10510_V1_20210612_A_01"), 
                    VIDEO_QUALITY = c("GOOD", "GOOD", 
                                      "GOOD", "GOOD", "GOOD", "GOOD"), 
                    VEGETATION = c("N", "Y", "N", 
                                   "Y", "N", "Y"), 
                    MUSSELS = c("N", "Y", "Y", "Y", "Y", "Y"), 
                    MUSSEL_ABUNDANCE = c("N", "Y", "N", "N", "N", "Y"), 
                    DOM_SUBSTRATE = c("S", "S", "S", "H", "S", "H"), 
                    HUMAN_FEATURES = c("N", "N", "N", "N", "N", "N"), 
                    FISH = c("N", "N", "N", "N", "N", "Y"), 
                    GOBIES = c("N", "N","N", "N", "N", "Y"), 
                    OTHER_FISH = c("N", "N", "N", "N", "N", "N"), 
                    NOTES2 = c("Interesting holes in the sediment", "Hard to see with plume of sediment", "", "Tiny clump of what looks like mussels, could be rocks? Need second opinion", 
                               "", "4-5 gobies"), 
                    COCR_COMMENTS = c(NA, NA, NA, NA, NA, NA)), 
               row.names = c(NA, 6L), class = "data.frame")
feat <- function(x){
  v %>% filter({{x}}=="Y")
}
feat(VEGETATION)
#>       SITE_ID VISIT_NO                     FILENAME VIDEO_QUALITY
#> 1 GBA20-10506        1 GBS20-10506_V1_20210608_B_01          GOOD
#> 2 GBA20-10508        1 GBS20-10508_V1_20210606_B_01          GOOD
#> 3 GBA20-10510        1 GBS20-10510_V1_20210612_A_01          GOOD
#>   VEGETATION MUSSELS MUSSEL_ABUNDANCE DOM_SUBSTRATE HUMAN_FEATURES FISH
#> 1          Y       Y                Y             S              N    N
#> 2          Y       Y                N             H              N    N
#> 3          Y       Y                Y             H              N    Y
#>   GOBIES OTHER_FISH
#> 1      N          N
#> 2      N          N
#> 3      Y          N
#>                                                                       NOTES2
#> 1                                         Hard to see with plume of sediment
#> 2 Tiny clump of what looks like mussels, could be rocks? Need second opinion
#> 3                                                                 4-5 gobies
#>   COCR_COMMENTS
#> 1            NA
#> 2            NA
#> 3            NA

Created on 2022-04-25 by the reprex package (v0.2.1)

1 Like

You're the best, FJCC! Thanks for all the R help you provide!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.