datazang
datazang

Reputation: 1159

Aggregate the data in R

I have a data set that is shown below:

library(tidyverse)
data <- tribble(
  ~category, ~product_id, 
  "A",         10, 
  "B",         20,    
  "C",         30, 
  "A",         10, 
  "A",         10, 
  "B",         20, 
  "C",         30, 
  "A",         10, 
  "A",         10, 
  "B",         20, 
)

And now, I want to group it by the "category" variable, keep the "product_id" and add a new variable that counts the categories:

  aggregated_data <- tribble(
  ~category, ~product_id, ~numberOfcategory
  "A",         10,       5,
  "B",         20,       3,   
  "C",         30,       2,
)

I already got the "numberOfcategory" with this code:

data %>%
  group_by(category) %>%
  tally(sort=TRUE) 

But somehow I could not keep the product_id.

Could someone help me to get the dataframe (aggregated_data)? Thanks in advance.

Upvotes: 1

Views: 59

Answers (1)

geoff
geoff

Reputation: 952

You were close! Just also group by product_id as follows:

data %>%
  group_by(category,product_id) %>%
  tally(sort=TRUE)

Upvotes: 1

Related Questions