Filter data > 0 in one column and == 0 in the rest

I need to obtain the exclusive asv in one column, i.e. asv_i that occurs only in sample MM2 (so it is "0" in the other samples), and so on for the rest of samples.
I’ve tried a solution like this, but I’m sure there is a much cleaner option, mostly because the number of samples is huge (I’ve posted a reduced version) . Thanks for any idea!!

asv_mm2 <- asv_table %>% 
  filter(MM2 > 0 & MM4 == 0  & MM6 == 0 & MM22 == 0 & MM26 == 0)

          MM2   MM4   MM6  MM22  MM26
asv_1    1454  1632  1210   108   834
asv_2   30428 29083 24248  5338  5686
asv_3    7299  5559  5741 14373 12186
asv_4     325   315   234    14   172
asv_5    2846  3852  2388 10551  7789
asv_6    1242  1258  1234   239  1020
asv_7     290   245   235   218   488

>Solution :

You could use if_all() and tidy selection to filter data with more concise presentation.

asv_table %>% 
  filter(MM2 > 0 & if_all(-MM2, ~ .x == 0))

# Also
# if_all(-MM2, `==`, 0)

Leave a Reply