简体   繁体   中英

Filter df according to multiple conditions with dyplr in R

I have a df structured like this:

  a.  b. 
  L.  1
  L.  2
  M.  3
  M.  2

"a" and "b" are columns names.

I want to filter the dataframe using dplyr. The conditions are: if (a == L & b > 1) and at the same time if (a == M & b > 2)

the result would be:

  a.  b. 
  L.  2
  M.  3

how can I do? thank you in advance!

>tibble(a=c('L','L','M','M'),b=c(1:3,2)) %>%
+   dplyr::filter((a == 'L' & b > 1) | (a == 'M' & b > 2))

# A tibble: 2 x 2
#  a         b
#  <chr> <dbl>
#1 L         2
#2 M         3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM