【发布时间】:2021-03-18 11:23:22
【问题描述】:
我想知道R Studio中这两个代码之间是否有任何区别:
any_data.frame %>% filter( Status == "F" & Edition == 2017)
和
any_data.frame %>% filter( Status == "F", Edition == 2017)
【问题讨论】:
-
请在询问之前阅读帮助文本。
我想知道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>
而使用布尔运算符|(或)会产生不同的结果:
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>
【讨论】: