Hi, andresrcs has kindly helped me to group elements in "Model" based on its frequency in the data frame. Now I'm trying to use something a bit more complicated. I would like to apply the rule but not based on overall frequency but frequency in 2020.
Basically, only "bb" and "cc" should stay (prop>=0.4), other Models should be coded as "Other".
The code below takes into account my data frame proportion rather than 2020 proportion.
library(dplyr)
library(forcats)
Sales.data.t <- data.frame(stringsAsFactors = FALSE,
Year = c(2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 2019),
Model = c("cc", "aa", "gg", "cc", "bb", "bb", NA,
"cc", "cc", "bb"),
RType = c("H", "A", "A", "H", "B", "h", "A", "H",
"H", "B")
)
prop <- with(Sales.data.t, table(Model, Year)) %>%
prop.table(margin = 2)
prop
Sales.data.t <- Sales.data.t %>%
mutate(Main.Models = fct_lump(Model, prop = 0.4))
Sales.data.t
Can you help please?