【发布时间】:2017-08-23 16:57:13
【问题描述】:
我正在使用 R 对几种产品进行交叉销售分析。 我已经转换了事务数据,它看起来像这样 -
df.articles <- cbind.data.frame(Art01,Art02,Art03)
Art01 Art02 Art03
bread yoghurt egg
butter bread yoghurt
cheese butter bread
egg cheese NA
potato NA NA
Actual data is 'data.frame': 69099 obs. of 33 variables.
我想获得与文章一起出售的所有不同文章及其计数的列表(在这种情况下例如面包或酸奶) 实际数据包含 56 篇文章,我需要检查与它一起销售的所有文章交叉销售。所以我想要的结果必须看起来像 -
Products sold with **bread** Products sold with **Yoghurt**
yoghurt 2 bread 2
egg 1 egg 1
cheese 1 butter 1
butter 1
.... and list goes on like this for say 52 different articles.
我已经尝试了几件事,但对于这个大数据集来说太手动了。 在 library(data.table) 的帮助下解决这个问题会很棒,如果没有,那也很好。 非常感谢您提前。
【问题讨论】:
-
考虑将数据重新格式化为具有两列结构的 data.frame,例如 data.frame(article=c(...), ingredients = c(...))。我认为您当前的 data.frame 效率很低
-
欢迎来到 StackOverflow!请阅读有关how to ask a good question 的信息以及如何提供reproducible example。这将使其他人更容易帮助您。
标签: r dplyr data.table