Apply Filter Across Multiple Columns

I have a simple two column dataframe.

> CodHorseMackerel
# A tibble: 2,352 x 2
   Gadus.morhua Trachurus.trachurus
          <dbl>               <dbl>
 1         84                    0 
 2          4                  386 
 3        100                    0 
 4         15                    0 
 5         36                   24 
 6          0                    4 
 7          8                  176 
 8         37.5                112.
 9          3                 1242 
10          2                  608

I want to filter and remove entries where values for both are 0. I do not want to remove all 0's, only rows where both Gadus.morhua and Trachurus.Trachurus = 0. I am struggling to apply the filter correctly.

I tried filter_all, and it didn't work correctly.

filter_all(CodHorseMackerel, any_vars(. < 1))

So I tried filter_if instead

filter_if(CodHorseMackerel, any_vars(. < 1))

Error in selected[[i]] <- eval_tidy(.p(column, ...)) : 
  more elements supplied than there are to replace

Help please!

plain old filter

filter(CodHorseMackerel,
  Gadus.morhua ==0 & Trachurus.trachurus ==0)

That made all values equal to 0, instead of removing zeroes. Now I have all zeroes!

yes sorry, classic mistake.
to do the opposite add the ! not signifier

filter(CodHorseMackerel,
 ! ( Gadus.morhua ==0 & Trachurus.trachurus ==0))
1 Like

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