Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting groups with shared elements

Tags:

r

I have a data set which looks like below:

Comp1  Product  Comp2
A       P1      B
A       P2      B
A       P3      B
C       P4      D
C       P2      D
X       P1      Y
X       P2      Y
X       P3      Y

So basically, Comp1 and Comp2 are the companies and Product is the product name that these companies have in common. I want the output to appear something like this:

Product Bundle    Count
P1,P2,P3          2
P2,P4             1

I am new to R and would appreciate any help in this case.

like image 872
Mili Avatar asked Jun 11 '26 10:06

Mili


1 Answers

Using dplyr, you can summarize the data then count it. For example

library(dplyr)

dd %>% arrange(Comp1, Product) %>% 
  group_by(Comp1) %>% 
  summarize(bundle=paste(unique(Product), collapse=",")) %>% 
  count(bundle)

#     bundle     n
#      <chr> <int>
# 1 P1,P2,P3     2
# 2    P2,P4     1

with the test data

dd <- read.table(text="Comp1  Product  Comp2
A       P1      B
A       P2      B
A       P3      B
C       P4      D
C       P2      D
X       P1      Y
X       P2      Y
X       P3      Y", header=TRUE)
like image 120
MrFlick Avatar answered Jun 14 '26 06:06

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!