我有以下数据集,并希望使用日期过滤,我可以使用任何包吗? 问题:每年展示最畅销和最畅销的产品(2013年,2014年)
Date_purchased Product_name
9-Jan-13 a
12-Jan-13 a
12-Jan-13 b
1-Feb-13 b
7-Feb-13 b
16-Dec-14 a
16-Dec-14 a
19-Dec-14 a
22-Dec-14 c
1-Jan-15 a
5-Jan-15 b
17-Jan-15 c
答案 0 :(得分:0)
对于畅销产品,我们可以尝试。
library(dplyr)
df1 %>%
group_by(year= format(as.Date(Date_purchased, '%d-%b-%y'), '%Y'),
Product_name) %>%
filter(year %in% 2013:2014) %>%
mutate(N= n()) %>%
group_by(year) %>%
summarise(Product_name = Product_name[which.max(N)])
# year Product_name
#1 2013 b
#2 2014 a
df1 <- structure(list(Date_purchased = c("9-Jan-13", "12-Jan-13",
"12-Jan-13",
"1-Feb-13", "7-Feb-13", "16-Dec-14", "16-Dec-14", "19-Dec-14",
"22-Dec-14", "1-Jan-15", "5-Jan-15", "17-Jan-15"),
Product_name = c("a", "a", "b", "b", "b", "a", "a", "a", "c", "a",
"b", "c")), .Names = c("Date_purchased", "Product_name"),
class = "data.frame", row.names = c(NA, -12L))
注意:将which.max
替换为which.min
,以获取最畅销产品。