Filtering with conditions

Hi everyone,

I have a dataset with 4 columns,

Product       count      Year   Customer
 1                    25          2020  A
 2                    10          2020  B
 2                    20          2019  A
 2                    35          2019  B
 1                    22          2018  A
 2                    18          2018  B

Year Ranges from 2015 to 2020, count is a standard integer number.
I could like to filter out products for all Years where the average 2020 count is higher than the average 2020 count. Something like this

If mean ```count``` where ```Year``` == 2020 > mean(count) delete row and for all of the same product in the previous years.

I hope this question makes sense.

Thanks

So would something like this work?

library(tidyverse)

df_group <- data.frame("Product" = c(1,2,2,2,1,2),
                       "Count" = c(25,10,20,35,22,18),
                       "Year" = c(2020,2020,2019,2019,2018,2018),
                       "Customer" = c("A","B","A","B","A","B"))


df_group %>% group_by(Year) %>% top_n(1, Count)
#> # A tibble: 3 x 4
#> # Groups:   Year [3]
#>   Product Count  Year Customer
#>     <dbl> <dbl> <dbl> <chr>   
#> 1       1    25  2020 A       
#> 2       2    35  2019 B       
#> 3       1    22  2018 A

Created on 2021-10-21 by the reprex package (v2.0.0)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.