I have a data set as this one:
data <- tribble(
~shop_name, ~category, ~NumberOfProducts,
"A", "Game", 50,
"A", "Book", 40,
"A", "Electronic", 30,
"B", "Home", 90,
"B", "Game", 100,
"B", "Electronic", 50,
"C", "Home", 60,
"C", "Book", 30,
"A", "Garden", 15,
"B", "Garden", 10,
)
But now, I want to create a new data frame something like this:
newdata <- tribble(
~shop_name, ~top_category,
"A", "Game, Book, Electronic",
"B", "Game, Home, Electronic",
"C", "Home, Book"
)
That means I want to group my data based on the "shop_name", and then I want to create a new variable (top_category) that shows the top three categories based on the ´NumberOfProducts`.
I've already tried to code it. But I got only top three when I coded like this:
data %>% top_n(3, NumberOfProducts)
Would someone help me to get the new data that shows the top three categories?