How to count the number of common values in a particular group?

孤人 提交于 2020-08-26 03:11:51

问题


I have a data frame df.

df <- data.frame(shop = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
                 product = c(1,2,3,4,5,1,5,2,4))

For every pair of shops i.e AB, AC and BC, I wish to count the number of common product.

The expected output is as follows

pair common_product
AB   1        # Because 1 is common
AC   1        # Because 2 is common
BC   2        # Because 5 and 4 is common

回答1:


A base R option would be :

pair <- combn(unique(df$shop), 2, paste0, collapse = "")
commmon_product <- combn(unique(df$shop), 2, function(x) 
      with(df, length(intersect(product[shop == x[1]], product[shop == x[2]]))))

data.frame(pair, commmon_product)
#  pair commmon_product
#1   AB               1
#2   AC               1
#3   BC               2

For every combination of shop values we find out how many product intersect using combn.




回答2:


use tidyverse

df %>% 
  group_by(product) %>% 
  summarise(pair = str_c(shop, collapse = "")) %>% 
  count(pair)

# A tibble: 4 x 2
  pair      n
  <chr> <int>
1 A         1
2 AB        1
3 AC        1
4 BC        2



回答3:


M1=matrix(0,length(unique(df$shop)),5)
for(i in 1:length(unique(df$shop))){
  M1[i,1]="A"
  M1[i,2]=dim(d[(df$shop)=="A",])[1]
  M1[i,3]=unique(df$shop)[i]
  M1[i,4]=dim(df[df$shop==unique(d$V1)[i],])[1]
  M1[i,5]=sum(df[(df$shop)=="A",2] %in% df[df$shop==unique(df$shop)[i],2])
}

It will return more than you want. Counts number of objects in "A","B" and "C"



来源:https://stackoverflow.com/questions/63259773/how-to-count-the-number-of-common-values-in-a-particular-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!