【问题标题】:Dplyr filter behaviour when using vector [duplicate]使用矢量时的 Dplyr 过滤器行为
【发布时间】:2022-02-11 18:57:27
【问题描述】:

我经常使用 dplyr 来处理数据,但我从未弄清楚使用 filter(df, variable == c(value1, value2) 时的 dplyr 过滤器行为

我们以iris 数据集为例。

library(dplyr)
data(iris)

# I want to filter by Species 'setosa' and 'versicolor'
# Solution 1
filter1 <- filter(iris, Species == 'setosa' | Species == 'versicolor')

nrow(filter1)
[1] 100 # expected result

# Solution 2
filter2 <- filter(iris, Species %in% c('setosa', 'versicolor'))

nrow(filter2)
[1] 100 # expected result

filter1 == filter2 # both solutions return the exact same result

#Solution 3
filter3 <- filter(iris, Species == c('setosa', 'versicolor'))

nrow(filter3)
[1] 50 # unexpected result

unique(filter3$Species)
[1] setosa     versicolor
Levels: setosa versicolor virginica

尽管Solution 3 正在过滤预期的物种,如unique(filter3$Species) 所示,但它只返回一半的出现(50 与Solution 1Solution2 中的100 相比)。对于Solution 3 中实际发生的情况,我将不胜感激。

【问题讨论】:

  • 它正在回收 c('setosa', 'versicolor') 以匹配 Species 的长度,因此它在 100 行中只有 50% 的时间与 Species 匹配。试试c("a", "b", "a", "b") == c("a", "b")c("a", "b", "b", "a") == c("a", "b") 看看有什么区别。
  • 你可以在这篇文章中看到详细信息:What is the difference between `%in%` and `==`?
  • 我明白了,谢谢您的评论!根据我的具体示例,在执行iris$Species == c('setosa', 'versicolor') 时,这种行为变得非常清楚!

标签: r dplyr filter


【解决方案1】:

filter(iris, Species == c("versicolor", "setosa")) 在直观上没有意义,因为一个 Species 不是 2 元组:

> "setosa" == c("setosa", "versicolor")
  [1]  TRUE FALSE

有趣的是,filter(iris, Species == c("setosa", "versicolor")) 产生相同的结果:将返回数据框的第一个 Species,因此降序排序将为您提供 versicolor:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

iris %>%
  as_tibble()
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # … with 140 more rows

iris %>%
  filter(Species == c('setosa', 'versicolor')) %>%
  as_tibble()
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          4.9         3            1.4         0.2 setosa 
#>  2          4.6         3.1          1.5         0.2 setosa 
#>  3          5.4         3.9          1.7         0.4 setosa 
#>  4          5           3.4          1.5         0.2 setosa 
#>  5          4.9         3.1          1.5         0.1 setosa 
#>  6          4.8         3.4          1.6         0.2 setosa 
#>  7          4.3         3            1.1         0.1 setosa 
#>  8          5.7         4.4          1.5         0.4 setosa 
#>  9          5.1         3.5          1.4         0.3 setosa 
#> 10          5.1         3.8          1.5         0.3 setosa 
#> # … with 40 more rows

iris %>%
  arrange(Species) %>%
  filter(Species == c('versicolor', 'setosa')) %>%
  as_tibble()
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          4.9         3            1.4         0.2 setosa 
#>  2          4.6         3.1          1.5         0.2 setosa 
#>  3          5.4         3.9          1.7         0.4 setosa 
#>  4          5           3.4          1.5         0.2 setosa 
#>  5          4.9         3.1          1.5         0.1 setosa 
#>  6          4.8         3.4          1.6         0.2 setosa 
#>  7          4.3         3            1.1         0.1 setosa 
#>  8          5.7         4.4          1.5         0.4 setosa 
#>  9          5.1         3.5          1.4         0.3 setosa 
#> 10          5.1         3.8          1.5         0.3 setosa 
#> # … with 40 more rows


iris %>%
  arrange(desc(Species)) %>%
  filter(Species == c('setosa', 'versicolor')) %>%
  as_tibble()
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#>  1          6.4         3.2          4.5         1.5 versicolor
#>  2          5.5         2.3          4           1.3 versicolor
#>  3          5.7         2.8          4.5         1.3 versicolor
#>  4          4.9         2.4          3.3         1   versicolor
#>  5          5.2         2.7          3.9         1.4 versicolor
#>  6          5.9         3            4.2         1.5 versicolor
#>  7          6.1         2.9          4.7         1.4 versicolor
#>  8          6.7         3.1          4.4         1.4 versicolor
#>  9          5.8         2.7          4.1         1   versicolor
#> 10          5.6         2.5          3.9         1.1 versicolor
#> # … with 40 more rows

iris %>%
  arrange(desc(Species)) %>%
  filter(Species == c('versicolor', 'setosa')) %>%
  as_tibble()
#> # A tibble: 50 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#>  1          7           3.2          4.7         1.4 versicolor
#>  2          6.9         3.1          4.9         1.5 versicolor
#>  3          6.5         2.8          4.6         1.5 versicolor
#>  4          6.3         3.3          4.7         1.6 versicolor
#>  5          6.6         2.9          4.6         1.3 versicolor
#>  6          5           2            3.5         1   versicolor
#>  7          6           2.2          4           1   versicolor
#>  8          5.6         2.9          3.6         1.3 versicolor
#>  9          5.6         3            4.5         1.5 versicolor
#> 10          6.2         2.2          4.5         1.5 versicolor
#> # … with 40 more rows

reprex package (v2.0.0) 于 2022-02-11 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多