r - 过滤时使用逗号“,”或符号&之间有什么区别吗?
问题描述
我想知道 R Studio 中这两个代码之间是否有任何区别:
any_data.frame %>% filter( Status == "F" & Edition == 2017)
和
any_data.frame %>% filter( Status == "F", Edition == 2017)
解决方案
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>
推荐阅读
- .net-core - 在 .NET 5 SDK 5.0.400 中找不到 Utf8JsonWriter.WriteRawValue()
- opencl - 如何构建具有多个 .cl 源的 OpenCL 程序
- python - “!” 键入后,按键盘上的“TAB”键。此方法在 Pycharm 中不起作用
- tableau-desktop - 如何在 Tableau 的嵌套 IF 内使用 sum?
- reactjs - 使用 netlify 和 monorepo 时反应路由器 dom 失败
- excel - Excel计算用逗号分隔的数字数量,范围从x到y
- arrays - rxjs 在间隔中为数组中的每个项目调用 2 个服务
- c++ - 如何将函数分配给已定义的已声明函数
- system-verilog - 子模块是由求解器独立刺激还是通过连接的顶层模块刺激?
- batch-file - 批量混合特殊字符故障?