【发布时间】:2017-11-16 06:55:27
【问题描述】:
当我过滤条件由其他过滤器组成的数据框时,它似乎不起作用。但是,如果我将条件存储为变量(示例中为f),则过滤工作正常。有人可以解释为什么会发生这种情况,以及如何使示例 2 之类的东西起作用吗?我宁愿不将过滤条件存储为变量。
library(dplyr)
# Dummy data set
df <- data.frame(Country = factor(c("Argentina", "Brazil", "Brazil", "Brazil")),
Type = factor(c("A", "A", "B", "C")))
# Only returns Brazil. No problem here.
f <- df %>%
group_by(Country) %>%
summarise(nTypes = n_distinct(Type)) %>%
filter(nTypes==3) %>%
select(Country) %>%
droplevels() %>%
unlist()
# > f
# Country
# Brazil
# Levels: Brazil
# Example 1 - Only returns rows of df where Country=="Brazil". No problem here.
df %>% filter(
Country %in% (f
)
)
# Country Type
# 1 Brazil A
# 2 Brazil B
# 3 Brazil C
# Example 2 - Filter is equivalent to `f` but returns all rows of df, not just Brazil. No idea why!
df %>% filter(
Country %in% (df %>%
group_by(Country) %>%
summarise(nTypes = n_distinct(Type)) %>%
filter(nTypes==3) %>%
select(Country) %>%
droplevels() %>%
unlist()
)
)
# Country Type
# 1 Argentina A
# 2 Brazil A
# 3 Brazil B
# 4 Brazil C
【问题讨论】:
-
您到底想得到什么 - 显示类型为 3 的国家/地区列表?
-
奇怪,我怀疑这是 NSE 名称冲突,但我不确定具体如何。无论如何,您可以将整个事情简化为
df %>% group_by(Country) %>% filter(n_distinct(Type) == 3) -
...或者只是翻转订单并将
f的代码输入{ filter(df, Country %in% .) } -
@amrrs 我试图获得与示例 1 相同的输出,但使用示例 2 中所示的方法。也就是说,不将过滤条件存储为变量
-
@alistaire 您的第一条评论效果很好!我想我可能过于复杂了。尽管如此,我的问题中的例子还是很奇怪。