比较R中的两个表,找出客户不购买的产品
我有两个表格如下:比较R中的两个表,找出客户不购买的产品,r,dplyr,R,Dplyr,我有两个表格如下: Cust_list <- data.frame( stringsAsFactors = FALSE, Customer = c("Mike S.","Tim P."), Type = c("Shoes","Socks"), Product_ID = c(233,6546) ) Product_Table <- data.frame( stringsAsFactors = FALSE
Cust_list <- data.frame(
stringsAsFactors = FALSE,
Customer = c("Mike S.","Tim P."),
Type = c("Shoes","Socks"),
Product_ID = c(233,6546)
)
Product_Table <- data.frame(
stringsAsFactors = FALSE,
Product_ID = c(233,256,296,8536,6546,8946),
Type = c("Shoes","Shoes","Shoes", "Socks","Socks","Socks")
)
Cust_list这是否有效:
library(dplyr)
library(tidyr)
Cust_list %>% full_join(Product_Table) %>% arrange(Type) %>%
fill(Customer,.direction = 'down') %>% anti_join(Cust_list)
Joining, by = c("Type", "Product_ID")
Joining, by = c("Customer", "Type", "Product_ID")
Customer Type Product_ID
1 Mike S. Shoes 256
2 Mike S. Shoes 296
3 Tim P. Socks 8536
4 Tim P. Socks 8946
你已经试过什么了?
library(dplyr)
library(tidyr)
Cust_list %>% full_join(Product_Table) %>% arrange(Type) %>%
fill(Customer,.direction = 'down') %>% anti_join(Cust_list)
Joining, by = c("Type", "Product_ID")
Joining, by = c("Customer", "Type", "Product_ID")
Customer Type Product_ID
1 Mike S. Shoes 256
2 Mike S. Shoes 296
3 Tim P. Socks 8536
4 Tim P. Socks 8946