在 R 中查找经常性采购
Find recurring purchasing in R
我想过滤我的重复购买数据。我的数据如下所示:
User Material Date
1 10001 20150119
1 10002 20150120
2 10003 20160121
2 10002 20181212
3 10004 20181110
1 10001 20150419
2 10003 20160421
2 10002 20180912
2 10002 20180612
等等....
现在我想按用户 material 以及谁更频繁地购买哪种产品来过滤数据,例如每年每 3 个月(季度)。
例如,我想要的输出如下所示:
User Material RecurringPurchasing
1 10001 2
2 10003 2
2 10002 3
因为输出我很灵活。那只是我的一个想法。不幸的是,我不知道如何描绘这样的东西。
这个怎么样:
library(tidyverse)
library(lubridate) # part of tidyverse but calling it out because of the quarter function
df <- read.table(header = TRUE, text = "
User Material Date
1 10001 20150119
1 10002 20150120
2 10003 20160121
2 10002 20181212
3 10004 20181110
1 10001 20150419
2 10003 20160421
2 10002 20180912
2 10002 20180612")
df %>%
mutate(quarter = quarter(Date)) %>%
count(User, Material, quarter) %>%
group_by(User, Material) %>%
summarize(RecurringPurchasing = sum(n)) %>%
filter(RecurringPurchasing > 1)
# A tibble: 3 x 3
# Groups: User [2]
User Material RecurringPurchasing
<int> <int> <int>
1 1 10001 2
2 2 10002 3
3 2 10003 2
我想过滤我的重复购买数据。我的数据如下所示:
User Material Date
1 10001 20150119
1 10002 20150120
2 10003 20160121
2 10002 20181212
3 10004 20181110
1 10001 20150419
2 10003 20160421
2 10002 20180912
2 10002 20180612
等等....
现在我想按用户 material 以及谁更频繁地购买哪种产品来过滤数据,例如每年每 3 个月(季度)。
例如,我想要的输出如下所示:
User Material RecurringPurchasing
1 10001 2
2 10003 2
2 10002 3
因为输出我很灵活。那只是我的一个想法。不幸的是,我不知道如何描绘这样的东西。
这个怎么样:
library(tidyverse)
library(lubridate) # part of tidyverse but calling it out because of the quarter function
df <- read.table(header = TRUE, text = "
User Material Date
1 10001 20150119
1 10002 20150120
2 10003 20160121
2 10002 20181212
3 10004 20181110
1 10001 20150419
2 10003 20160421
2 10002 20180912
2 10002 20180612")
df %>%
mutate(quarter = quarter(Date)) %>%
count(User, Material, quarter) %>%
group_by(User, Material) %>%
summarize(RecurringPurchasing = sum(n)) %>%
filter(RecurringPurchasing > 1)
# A tibble: 3 x 3
# Groups: User [2]
User Material RecurringPurchasing
<int> <int> <int>
1 1 10001 2
2 2 10002 3
3 2 10003 2