R 中的购物篮分析
Christopher Bruffaerts
Statistician
购物篮课程

购物篮 = 商品集合
商品

购物篮示例:
你在超市的购物篮
你的亚马逊购物车
你在 DataCamp 的课程
你在 Netflix 看过的电影
商店里有哪些?

今天准备买什么?

商店里有哪些?
store = c("Bread", "Butter",
"Cheese", "Wine")
set.seed(1234)
n_items = 4
my_basket = data.frame(
TID = rep(1,n_items),
Product = sample(
store, n_items,
replace = TRUE))
R 输出
my_basket
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
我的原始购物篮
每个购买的商品一条记录
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
我的调整后购物篮
每个不同商品一条记录
# A tibble: 2 x 3
TID Product Quantity
<dbl> <fct> <int>
1 1 Bread 1
2 1 Cheese 3
重塑购物篮数据
# 调整我的购物篮
my_basket = my_basket %>%
add_count(Product) %>%
unique() %>%
rename(Quantity = n)
# 不同商品数量
n_distinct(my_basket$Product)
2
# 购物篮总量
my_basket %>% summarize(sum(Quantity))
4
可视化购物篮中的商品
# 绘制商品
ggplot(my_basket,
aes(x=reorder(Product, Quantity),
y = Quantity)) +
geom_col() +
coord_flip() +
xlab("Items") +
ggtitle("Summary of items
in my basket")

问题:篮子内的商品之间有关系吗?

回到示例
你在超市的购物篮, 例如:意大利面和番茄酱
你的亚马逊购物车, 例如:手机和手机壳
你在 DataCamp 的课程, 例如:"Introduction to R"和"Intermediate R"
R 中的购物篮分析