dplyr: select by name and value at the same time

Ken*_*ams 3 select r dplyr

(This question is probably a duplicate, but I can't find it being asked yet...)

Using dplyr techniques, how can I select columns from a data.frame by both names & values at the same time? For example the following (which doesn't work):

> data.frame(x=4, y=6, z=3) %>%
    select_if(matches('x') | mean(.) > 5)
Error: No tidyselect variables were registered
Run Code Online (Sandbox Code Playgroud)

In base R, I would do something like this:

> df <- data.frame(x=4, y=6, z=3)
> df[names(df) == 'x' | colMeans(df) > 5]
  x y
1 4 6
Run Code Online (Sandbox Code Playgroud)

And*_*rew 6

您可以使用select逗号和colMeans

data.frame(x=4, y=6, z=3) %>%
  select(matches("x"), which(colMeans(.) > 5))
  x y
1 4 6
Run Code Online (Sandbox Code Playgroud)