我有两张表如下:
Cust_list <- data.frame(
stringsAsFactors = FALSE,
Customer = c("Mike S.","Tim P.","Steve Z."),
Type = c("Shoes","Socks", "Shoes"),
Product_ID = c(233,6546,296)
)
Product_Table <- data.frame(
stringsAsFactors = FALSE,
Product_ID = c(233,256,296,8536,6546,8946),
Type = c("Shoes","Shoes","Shoes", "Socks","Socks","Socks"),
SubType = c("Basketball", "Basketball", "Baseball", "Ankle", "Knee-High", "Mid")
)我希望识别“客户”在“类型”内没有购买的"Product_ID“。
例如,Steve Z.购买了Product = "296“和Type = "Shoes”,而没有购买Product = "233“和"256”。由于Steve .没有购买Type = "Socks",这将不包括在输出中。
另外,如果客户购买与SubType =“篮球”相关的产品ID,请不要输出与“篮球”相关的产品ID。由于Mike已经购买了篮球鞋,与篮球鞋相关的其他产品ID不包括在输出中。
输出表在下面。
在这里输入图像描述

发布于 2021-06-03 19:47:55
基于Onyambu为原始帖子提供的答案,您可以如下所示。
library(dplyr)
out <- Cust_list %>%
left_join(Product_Table) %>%
select(Customer, SubType) %>%
filter(SubType == 'Basketball')
select(Cust_list, -Product_ID) %>%
left_join(Product_Table) %>%
anti_join(Cust_list) %>%
anti_join(out)
# Customer Type Product_ID SubType
# 1 Mike S. Shoes 296 Baseball
# 2 Tim P. Socks 8536 Ankle
# 3 Tim P. Socks 8946 Mid
# 4 Steve Z. Shoes 233 Basketball
# 5 Steve Z. Shoes 256 Basketballhttps://stackoverflow.com/questions/67826184
复制相似问题