Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr::select_if can use colnames and their values at the same time?

Tags:

r

dplyr

I want to select cols using colnames and their values in a single pipe chain without referring other objects, such as NAMES <- names(d). Can I do it with select_if() ?

For example,

I can use colnames to select cols.
(select(matches(...)) is smarter treating only colnames).

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, don't run).

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

Any help would be greatly appreciated.

like image 262
cuttlefish44 Avatar asked Dec 30 '17 10:12

cuttlefish44


1 Answers

A workaround that is not too complicated is:

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

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

Which 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
like image 120
mt1022 Avatar answered Nov 16 '22 12:11

mt1022