0

I am trying to create a plot with the frequency of a factor. But I have 70 different factors. That is why I would like to create an 'other' group for everything that is not Top10.

It is currently working with the top_n() function to create the top 10.

ProblemFrequency <- count(Disruptions, 'Problem')
Top10 <- top_n(ProblemFrequency, n=10, freq)

And I already got 'other' grouping to work by using within() in combination with an if-else statement

ProblemFrequency <- within(ProblemFrequency, other.group <- ifelse(freq > 100, as.character(Problem), "Other"))
ProblemFrequency <- within(ProblemFrequency, relevel(factor(other.group), "Other"))

I have these two pieces working separately but I can't seem to figure out how to combine them. I am working with a shiny app where users can filter, the Top10 and their frequency is constantly changing, so I can't just use an equation.

I think the if statement should check if the factor is in the Top10. If not: group it into other.

camille
  • 16,432
  • 18
  • 38
  • 60
Anoux
  • 47
  • 7
  • 3
    `forcats::fct_lump` handles this use case nicely. – Jon Spring Oct 29 '19 at 20:37
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 29 '19 at 20:43

0 Answers0