首页 > 解决方案 > 过滤时使用逗号“,”或符号&之间有什么区别吗?

问题描述

我想知道 R Studio 中这两个代码之间是否有任何区别:

any_data.frame %>% filter( Status == "F" & Edition == 2017)

any_data.frame %>% filter( Status == "F", Edition == 2017)

标签: rdplyr

解决方案


filter在函数中使用逗号类似于&,我们可以通过以下示例对其进行研究:

starwars %>%
  filter(height > 150, homeworld == "Tatooine")

# A tibble: 9 x 14
  name   height  mass hair_color skin_color eye_color birth_year sex   gender homeworld species
  <chr>   <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>     <chr>  
1 Luke ~    172    77 blond      fair       blue            19   male  mascu~ Tatooine  Human  
2 C-3PO     167    75 NA         gold       yellow         112   none  mascu~ Tatooine  Droid  
3 Darth~    202   136 none       white      yellow          41.9 male  mascu~ Tatooine  Human  
4 Owen ~    178   120 brown, gr~ light      blue            52   male  mascu~ Tatooine  Human  
5 Beru ~    165    75 brown      light      blue            47   fema~ femin~ Tatooine  Human  
6 Biggs~    183    84 black      light      brown           24   male  mascu~ Tatooine  Human  
7 Anaki~    188    84 blond      fair       blue            41.9 male  mascu~ Tatooine  Human  
8 Shmi ~    163    NA black      fair       brown           72   fema~ femin~ Tatooine  Human  
9 Clieg~    183    NA brown      fair       blue            82   male  mascu~ Tatooine  Human  
# ... with 3 more variables: films <list>, vehicles <list>, starships <list>


starwars %>%
  filter(height > 150 & homeworld == "Tatooine")

# A tibble: 9 x 14
  name   height  mass hair_color skin_color eye_color birth_year sex   gender homeworld species
  <chr>   <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>     <chr>  
1 Luke ~    172    77 blond      fair       blue            19   male  mascu~ Tatooine  Human  
2 C-3PO     167    75 NA         gold       yellow         112   none  mascu~ Tatooine  Droid  
3 Darth~    202   136 none       white      yellow          41.9 male  mascu~ Tatooine  Human  
4 Owen ~    178   120 brown, gr~ light      blue            52   male  mascu~ Tatooine  Human  
5 Beru ~    165    75 brown      light      blue            47   fema~ femin~ Tatooine  Human  
6 Biggs~    183    84 black      light      brown           24   male  mascu~ Tatooine  Human  
7 Anaki~    188    84 blond      fair       blue            41.9 male  mascu~ Tatooine  Human  
8 Shmi ~    163    NA black      fair       brown           72   fema~ femin~ Tatooine  Human  
9 Clieg~    183    NA brown      fair       blue            82   male  mascu~ Tatooine  Human  
# ... with 3 more variables: films <list>, vehicles <list>, starships <list>

而使用布尔运算符|(or) 将产生不同的结果:

starwars %>%
  filter(height > 150 | homeworld == "Tatooine")

# A tibble: 70 x 14
   name  height  mass hair_color skin_color eye_color birth_year sex   gender homeworld species
   <chr>  <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>     <chr>  
 1 Luke~    172    77 blond      fair       blue            19   male  mascu~ Tatooine  Human  
 2 C-3PO    167    75 NA         gold       yellow         112   none  mascu~ Tatooine  Droid  
 3 Dart~    202   136 none       white      yellow          41.9 male  mascu~ Tatooine  Human  
 4 Owen~    178   120 brown, gr~ light      blue            52   male  mascu~ Tatooine  Human  
 5 Beru~    165    75 brown      light      blue            47   fema~ femin~ Tatooine  Human  
 6 R5-D4     97    32 NA         white, red red             NA   none  mascu~ Tatooine  Droid  
 7 Bigg~    183    84 black      light      brown           24   male  mascu~ Tatooine  Human  
 8 Obi-~    182    77 auburn, w~ fair       blue-gray       57   male  mascu~ Stewjon   Human  
 9 Anak~    188    84 blond      fair       blue            41.9 male  mascu~ Tatooine  Human  
10 Wilh~    180    NA auburn, g~ fair       blue            64   male  mascu~ Eriadu    Human  
# ... with 60 more rows, and 3 more variables: films <list>, vehicles <list>, starships <list>

推荐阅读