0

How to subset a data frame based on "OR" condition in two columns? How can I use tidyverse?

I can do this by left join. Just wondering if there is any elegant way to do this better, since I have more than 2000 raws.

for an example like this one below enter image description here

Dendrobium
  • 323
  • 5
  • 13
  • 3
    `df %>% filter(ID == "a" | Rep == 1)`. `|` is "OR" when comparing logical vectors. – Marius Sep 25 '19 at 04:47
  • 1
    Try `subset(df1, ID %in% c('a', 'b') & Rep %in% 1:2)` – akrun Sep 25 '19 at 04:47
  • Wow, thanks a heap @Marius – Dendrobium Sep 25 '19 at 04:47
  • https://stackoverflow.com/a/30921776/496803 for the dplyr specific part of the duplicate. – thelatemail Sep 25 '19 at 04:50
  • Please [use text, not images/links, for text--including tables & ERDs.](https://meta.stackoverflow.com/q/285551/3404097) Use images only for what cannot be expressed as text or to augment text. Images cannot be searched for or cut & pasted. Include a legend/key & explanation with an image. – philipxy Sep 25 '19 at 05:59
  • This is a faq. Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. We cannot reason, communicate or search unless we make the effort to (re-re-re-)write clearly. – philipxy Sep 25 '19 at 05:59

1 Answers1

1

We can use subset from base R

subset(df1, ID %in% c('a', 'b') & Rep %in% 1:2)
akrun
  • 874,273
  • 37
  • 540
  • 662