简体   繁体   English

当满足其他列的条件时返回所有行 [R]

[英]Return all rows when a condition is met for other columns [R]

Say I wanted to ask this question:假设我想问这个问题:

Which individuals are from planets and genders where there are any individuals that have blond hair and blue eyes?哪些人来自金发和蓝眼睛的人的行星和性别?

I can do this easily enough in two steps.我可以通过两个步骤轻松完成此操作。

  1. create subset of individuals with blond hair and blue eyes创建具有金发蓝眼睛的个体子集
  2. use the planets and genders present there to do another filter使用那里的行星和性别做另一个过滤器

If there a way that I can do this in one step using a dplyr-like approach?如果有一种方法可以使用类似 dplyr 的方法一步完成?

library(dplyr, warn.conflicts = FALSE)
any_blond_blue <- starwars %>%
  group_by(homeworld, gender) %>%
  filter(eye_color == "blue", hair_color == "blond")


starwars %>%
  filter(
    homeworld %in% any_blond_blue$homeworld,
    gender %in% any_blond_blue$gender
  )
#> # A tibble: 9 × 14
#>   name      height  mass hair_color skin_color eye_color birth_year sex   gender
#>   <chr>      <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
#> 1 Luke Sky…    172    77 blond      fair       blue            19   male  mascu…
#> 2 C-3PO        167    75 <NA>       gold       yellow         112   none  mascu…
#> 3 Darth Va…    202   136 none       white      yellow          41.9 male  mascu…
#> 4 Owen Lars    178   120 brown, gr… light      blue            52   male  mascu…
#> 5 R5-D4         97    32 <NA>       white, red red             NA   none  mascu…
#> 6 Biggs Da…    183    84 black      light      brown           24   male  mascu…
#> 7 Anakin S…    188    84 blond      fair       blue            41.9 male  mascu…
#> 8 Finis Va…    170    NA blond      fair       blue            91   male  mascu…
#> 9 Cliegg L…    183    NA brown      fair       blue            82   male  mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> #   vehicles <list>, starships <list>

Instead of filter ing to create a new dataset, do the filter within the filter itself ie subset the values of 'homeworld', 'gender' based on the logical expression ( eye_color == 'blue' & hair_color == 'blond' ), then use %in% to create the logical expression for 'homeworld' and 'gender' to be used in filter与其filter创建新数据集,不如在filter器本身内进行filter ,即根据逻辑表达式( eye_color == 'blue' & hair_color == 'blond' )对 'homeworld'、'gender' 的值进行子集化,然后使用%in%创建用于filter的 'homeworld' 和 'gender' 的逻辑表达式

library(dplyr)
starwars %>% 
  filter(homeworld %in% homeworld[eye_color == 'blue' & hair_color == 'blond'], 
   gender %in% gender[eye_color == 'blue' & hair_color == 'blond']) 

-output -输出

# A tibble: 9 × 14
  name  height  mass hair_color skin_color eye_color birth_year sex   gender homeworld species films vehicles starships
  <chr>  <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>     <chr>   <lis> <list>   <list>   
1 Luke…    172    77 blond      fair       blue            19   male  mascu… Tatooine  Human   <chr> <chr>    <chr [2]>
2 C-3PO    167    75 <NA>       gold       yellow         112   none  mascu… Tatooine  Droid   <chr> <chr>    <chr [0]>
3 Dart…    202   136 none       white      yellow          41.9 male  mascu… Tatooine  Human   <chr> <chr>    <chr [1]>
4 Owen…    178   120 brown, gr… light      blue            52   male  mascu… Tatooine  Human   <chr> <chr>    <chr [0]>
5 R5-D4     97    32 <NA>       white, red red             NA   none  mascu… Tatooine  Droid   <chr> <chr>    <chr [0]>
6 Bigg…    183    84 black      light      brown           24   male  mascu… Tatooine  Human   <chr> <chr>    <chr [1]>
7 Anak…    188    84 blond      fair       blue            41.9 male  mascu… Tatooine  Human   <chr> <chr>    <chr [3]>
8 Fini…    170    NA blond      fair       blue            91   male  mascu… Coruscant Human   <chr> <chr>    <chr [0]>
9 Clie…    183    NA brown      fair       blue            82   male  mascu… Tatooine  Human   <chr> <chr>    <chr [0]>

Or slightly more compact and general approach would be using if_all/if_any as it can be applied to multiple columns.或者稍微更紧凑和更通用的方法是使用if_all/if_any ,因为它可以应用于多个列。 Here, we use if_all so that it returns TRUE only when both column conditions are TRUE在这里,我们使用if_all以便它仅在两个列条件都为 TRUE 时才返回 TRUE

starwars %>%
   filter(if_all(c(homeworld, gender),
    ~ .x %in% .x[eye_color == 'blue' & hair_color == 'blond']))

-output -输出

# A tibble: 9 × 14
  name  height  mass hair_color skin_color eye_color birth_year sex   gender homeworld species films vehicles starships
  <chr>  <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>     <chr>   <lis> <list>   <list>   
1 Luke…    172    77 blond      fair       blue            19   male  mascu… Tatooine  Human   <chr> <chr>    <chr [2]>
2 C-3PO    167    75 <NA>       gold       yellow         112   none  mascu… Tatooine  Droid   <chr> <chr>    <chr [0]>
3 Dart…    202   136 none       white      yellow          41.9 male  mascu… Tatooine  Human   <chr> <chr>    <chr [1]>
4 Owen…    178   120 brown, gr… light      blue            52   male  mascu… Tatooine  Human   <chr> <chr>    <chr [0]>
5 R5-D4     97    32 <NA>       white, red red             NA   none  mascu… Tatooine  Droid   <chr> <chr>    <chr [0]>
6 Bigg…    183    84 black      light      brown           24   male  mascu… Tatooine  Human   <chr> <chr>    <chr [1]>
7 Anak…    188    84 blond      fair       blue            41.9 male  mascu… Tatooine  Human   <chr> <chr>    <chr [3]>
8 Fini…    170    NA blond      fair       blue            91   male  mascu… Coruscant Human   <chr> <chr>    <chr [0]>
9 Clie…    183    NA brown      fair       blue            82   male  mascu… Tatooine  Human   <chr> <chr>    <chr [0]>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM