Dplyr :: select_if can use code names and their values ​​at the same time?

I want to select cols using colnames and their values ​​in one channel chain without reference to other objects, for example NAMES <- names(d) . Can I do this with select_if() ?

For instance,

I can use colnames to select cols.
( select(matches(...)) smarter than only calling).

 library(dplyr) d <- iris %>% select(-Species) %>% tibble::as.tibble() d %>% select_if(stringr::str_detect(names(.), "Petal")) 

And I can use the values.

 d %>% select_if(~ mean(.) > 5) 

But how to use both of them? (especially OR)
Below code is what I want (of course not to run).

 d %>% select_if(stringr::str_detect(names(.), "Petal") | ~ mean(.) > 5) 

Any help would be greatly appreciated.

+5
source share
1 answer

A workaround that is not too complicated is this:

 d %>% select_if(stringr::str_detect(names(.), "Petal") | sapply(., mean) > 5) # or d %>% select_if(grepl("Petal",names(.)) | sapply(., mean) > 5) 

What gives:

 # 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 
+7
source

Source: https://habr.com/ru/post/1274394/


All Articles