I have the following data frame
library(dplyr)
dat3=data.frame(reach=c('T','T', 'T', 'T','C','C','C','C'), metric=c("% cover", "% rock", "% cover", "% rock", "% cover", "% rock", "% cover", "% rock"),
Value=c(0,1,1,0,1,0,1,0),site=c(1,1,2,2,1,1,2,2) )
dat3 <- dat3 |>
group_by(site,metric)
dat3
#> # A tibble: 8 × 4
#> # Groups: site, metric [4]
#> reach metric Value site
#> <chr> <chr> <dbl> <dbl>
#> 1 T % cover 0 1
#> 2 T % rock 1 1
#> 3 T % cover 1 2
#> 4 T % rock 0 2
#> 5 C % cover 1 1
#> 6 C % rock 0 1
#> 7 C % cover 1 2
#> 8 C % rock 0 2
Created on 2024-05-07 with reprex v2.1.0
I have been trying to use a tidyverse approach to achieve the desired result. What I would like is for any case where Site has a 0 value for BOTH reaches for ANY metric it is filtered OUT of the data frame. In this example above I would end up with this data frame:
#> # A tibble: 6 × 4
#> # Groups: site, metric [4]
#> reach metric Value site
#> <chr> <chr> <dbl> <dbl>
#> 1 T % cover 0 1
#> 2 T % rock 1 1
#> 3 T % cover 1 2
#> 4 C % cover 1 1
#> 5 C % rock 0 1
#> 6 C % cover 1 2
I have tried group_by (Site, Metric) then have tried various ways to select for cases where value = 0 for BOTH Reach values(T and C) but have not been successful. This is an example data.frame. In reality there are 100's of unique Sites and each have a T and C value or Reach and there are 50 different metrics for each Site (a metric value for every T and C of a site). Hopefully that makes some sense!
Does anyone have any ideas on how to achieve the desired result using a tidyverse approach?
Created on 2024-05-07 with reprex v2.1.0