我的数据集设置如下:
User Day
10 2
1 3
15 1
3 1
1 2
15 3
1 1
我试图找出这三天都在场的用户。我正在使用以下使用dplyr软件包的代码:
MAU%>%
group_by(User)%>%
filter(c(1,2,3) %in% Day)
# but get this error message:
# Error in filter_impl(.data, quo) : Result must have length 12, not 3
知道如何解决?
答案 0 :(得分:3)
使用末尾注释中可重复显示的输入,计算不同的用户并过滤出3天的用户:
library(dplyr)
DF %>%
distinct %>%
count(User) %>%
filter(n == 3) %>%
select(User)
给予:
# A tibble: 1 x 1
User
<int>
1 1
Lines <- "
User Day
10 2
1 3
15 1
3 1
1 2
15 3
1 1"
DF <- read.table(text = Lines, header = TRUE)
答案 1 :(得分:2)
我们可以使用all
从逻辑向量1:3 %in% Day
中获得一个TRUE / FALSE
library(dplyr)
MAU %>%
group_by(User)%>%
filter(all(1:3 %in% Day))
# A tibble: 3 x 2
# Groups: User [1]
# User Day
# <int> <int>
#1 1 3
#2 1 2
#3 1 1
MAU <- structure(list(User = c(10L, 1L, 15L, 3L, 1L, 15L, 1L), Day = c(2L,
3L, 1L, 1L, 2L, 3L, 1L)), class = "data.frame", row.names = c(NA,
-7L))