Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204771

dplyr:: How to use SE and `%in%` in `filter`?

$
0
0

Consider

df = data.frame(col1 = 1:3, col2 = 2:4)

I want to do

df %>%
  filter(col1 %in% 2:3)

which should return the 2nd and 3rd row, but col1 is passed in as a string as variable str_1 = "col1"

So I tried

  str_1 = "col1"
  df %>% 
    filter(!!!str_1 %in% 2:3)

  df %>% 
    filter(!!str_1 %in% 2:3)

and niether worked. What's the right syntax for this? And what's an easier way to remember these dplyr tricks?


Viewing all articles
Browse latest Browse all 204771

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>