What is a way to apply the fct_lump_min
within a group_by
pipe? Meaning, I have a set of data where I want to group by two variables (year and group) and count the frequency (n). I also want to lump the variables such that for each year, the count of group is lumped where n < 5.
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.2.2
#> Warning: package 'ggplot2' was built under R version 4.2.2
#> Warning: package 'tibble' was built under R version 4.2.2
#> Warning: package 'tidyr' was built under R version 4.2.2
#> Warning: package 'readr' was built under R version 4.2.2
#> Warning: package 'purrr' was built under R version 4.2.2
#> Warning: package 'dplyr' was built under R version 4.2.2
#> Warning: package 'stringr' was built under R version 4.2.2
#> Warning: package 'forcats' was built under R version 4.2.2
# sample data
set.seed(1234)
sample_data <- tibble(
year = sample(seq(2001, 2005, 1), size = 100, replace = TRUE),
group = sample(c("apple", "berry", "carrot", "fig"), size = 100, replace = TRUE),
value = 1
)
# idea for what I want to accomplish
sample_data %>%
group_by(
year,
group = fct_lump_min(
f = group,
min = 5
)) %>%
count()
#> # A tibble: 20 × 3
#> # Groups: year, group [20]
#> year group n
#> <dbl> <fct> <int>
#> 1 2001 apple 5
#> 2 2001 berry 5
#> 3 2001 carrot 4
#> 4 2001 fig 5
#> 5 2002 apple 3
#> 6 2002 berry 2
#> 7 2002 carrot 8
#> 8 2002 fig 6
#> 9 2003 apple 5
#> 10 2003 berry 7
#> 11 2003 carrot 5
#> 12 2003 fig 4
#> 13 2004 apple 1
#> 14 2004 berry 5
#> 15 2004 carrot 10
#> 16 2004 fig 6
#> 17 2005 apple 8
#> 18 2005 berry 3
#> 19 2005 carrot 4
#> 20 2005 fig 4
# target output
desired_output <- tribble(
~"year", ~"group", ~"n",
2001, "apple", 5,
2001, "berry", 5,
2001, "fig", 5,
2001, "Other", 4,
2002, "carrot", 8,
2002, "fig", 6,
2002, "Other", 5,
2003, "apple", 5,
2003, "berry", 7,
2003, "carrot", 5,
2003, "Other", 4,
2004, "berry", 5,
2004, "carrot", 10,
2004, "fig", 6,
2004, "Other", 1,
2005, "apple", 8,
2005, "Other", 11,
)
Created on 2023-03-10 with reprex v2.0.2