rfilterdplyrtidyverseremove-if

Two condition filtering and removing Zero Values


I have a simple dataframe of counts in a growing pepper experiment. I want to remove observations where both treatment's[(Control and Covered) Fruit_total are equal to zero. I tried filter but I can only handle one variable at a time. Any advice?

enter image description here]2


Solution

  • You can accomplish this by grouping on location.id and filtering for the sum of Fruit_total:

    library(tidyverse)
    
    df %>%
      group_by(location.ID) %>%
      filter(sum(Fruit_total) != 0)
    

    Yields:

    # A tibble: 22 x 5
    # Groups:   location.ID [11]
       location.ID  Year plant                 treatment Fruit_total
             <dbl> <dbl> <chr>                 <chr>           <dbl>
     1           7  2019 Anaheim.Peppers.Count Control            23
     2           9  2019 Anaheim.Peppers.Count Control             3
     3          15  2019 Anaheim.Peppers.Count Control             0
     4          23  2019 Anaheim.Peppers.Count Control             1
     5          38  2019 Anaheim.Peppers.Count Control             8
     6          41  2019 Anaheim.Peppers.Count Control             1
     7          42  2019 Anaheim.Peppers.Count Control            12
     8          43  2019 Anaheim.Peppers.Count Control             7
     9          45  2019 Anaheim.Peppers.Count Control             5
    10          49  2019 Anaheim.Peppers.Count Control            13
    # ... with 12 more rows