我想在单个管道链中使用colnames及其值来选择cols而不引用其他对象,例如NAMES <- names(d).我能用select_if()吗?
例如,
我可以使用colnames来选择cols.
(select(matches(...))更聪明地处理colnames).
library(dplyr)
d <- iris %>% select(-Species) %>% tibble::as.tibble()
d %>% select_if(stringr::str_detect(names(.), "Petal"))
Run Code Online (Sandbox Code Playgroud)
我可以使用这些值.
d %>% select_if(~ mean(.) > 5)
Run Code Online (Sandbox Code Playgroud)
但是如何使用它们呢?(特别是OR)
下面的代码是我想要的(当然,不要运行).
d %>% select_if(stringr::str_detect(names(.), "Petal") | ~ mean(.) > 5)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
一个不太复杂的解决方法是:
d %>% select_if(stringr::str_detect(names(.), "Petal") | sapply(., mean) > 5)
# or
d %>% select_if(grepl("Petal",names(.)) | sapply(., mean) > 5)
Run Code Online (Sandbox Code Playgroud)
这使:
# A tibble: 150 x 3
Sepal.Length Petal.Length Petal.Width
<dbl> <dbl> <dbl>
1 5.1 1.4 0.2
2 4.9 1.4 0.2
3 4.7 1.3 0.2
4 4.6 1.5 0.2
5 5.0 1.4 0.2
6 5.4 1.7 0.4
7 4.6 1.4 0.3
8 5.0 1.5 0.2
9 4.4 1.4 0.2
10 4.9 1.5 0.1
# ... with 140 more rows
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2099 次 |
| 最近记录: |