Product usage combination in R

纵然是瞬间 提交于 2019-12-11 14:25:08

问题


I trying to figure out a way to get a list of product combinations with unique users in R. This is a follow up question to [Generate matrix of unique user-item cross-product combinations

df <- data.frame(Products=c('Product a', 'Product b', 'Product a', 
                            'Product c', 'Product b', 'Product c', 'Product d'),
                 Users=c('user1', 'user1', 'user2', 'user1', 
                         'user2','user3', 'user1'))

Output of df is:

   Products Users
1 Product a user1
2 Product b user1
3 Product a user2
4 Product c user1
5 Product b user2
6 Product c user3
7 Product d user1

The output I am looking for would be all three product combinations:

Product a/Product b/Product c - 3
Product a/Product b/Product d - 2
Product b/Product c/Product d - 3
...

Thanks again for your help.


回答1:


It looks like you want logical-OR treatment as the relation between users and each product set. In other words, you want to count how many unique users have any product in the set. Here's one way of doing it:

df <- data.frame(Products=c('Product a','Product b','Product a','Product c','Product b','Product c','Product d'),Users=c('user1','user1','user2','user1','user2','user3','user1'));
comb <- combn(unique(df$Products),3);
data.frame(comb=apply(comb,2,function(x) paste(levels(comb)[x],collapse='/')),num=apply(comb,2,function(x) length(unique(df$Users[as.integer(df$Products)%in%x]))));
##                            comb num
## 1 Product a/Product b/Product c   3
## 2 Product a/Product b/Product d   2
## 3 Product a/Product c/Product d   3
## 4 Product b/Product c/Product d   3

Edit: Logical-AND is trickier, since we need to test for the presence of every product for every user. I think I found a good solution using aggregate() and match():

data.frame(comb=apply(comb,2,function(x) paste(levels(comb)[x],collapse='/')),num=apply(comb,2,function(x) sum(aggregate(Products~Users,df,function(y) !any(is.na(match(x,as.integer(y)))))$Products)));
##                            comb num
## 1 Product a/Product b/Product c   1
## 2 Product a/Product b/Product d   1
## 3 Product a/Product c/Product d   1
## 4 Product b/Product c/Product d   1


来源:https://stackoverflow.com/questions/31092962/product-usage-combination-in-r

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