Reputation: 1169
I've a dataset as shown below:
product_list <- tribble(
~shop_name, ~product_id, ~category,
"A", 1, "Game",
"B", 2, "Book",
"C", 3, "Electronic",
"A", 4, "Home",
"A", 5, "Game",
"B", 3, "Electronic",
"C", 8, "Home",
"A", 6, "Book",
"A", 7, "Game",
"B", 7, "Game",
)
But now I want to add a new column that shows us the number of categories per each shop and want to keep all other columns. Here is the desired result:
desired_list <- tribble(
~shop_name, ~product_id, ~category, ~number_of_category
"A", 1, "Game", 3,
"B", 2, "Book", 1,
"C", 3, "Electronic", 1,
"A", 4, "Home", 1,
"A", 5, "Game", 3,
"B", 3, "Electronic", 1,
"C", 8, "Home", 1,
"A", 6, "Book", 1,
"A", 7, "Game", 3,
"B", 7, "Game", 1,
)
Can someone help me to get the result?
Thanks in advance.
Upvotes: 0
Views: 44
Reputation: 4658
You can add counts with the add_count
function, which, unlike count
does not remove the other columns:
product_list %>% add_count(shop_name, category, name = "number_of_category")
Upvotes: 1