(这个问题可能是重复的,但我还找不到它被问到的......)

使用 dplyr 技术,如何同时通过名称和值从 data.frame 中选择列?例如以下(不起作用):

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

在基础 R 中,我会做这样的事情:

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

最佳答案

更新: 使用 dplyr v1.0.0 :

data.frame(x=4, y=6, z=3) %>%
      select(matches("x"), where(~mean(.) > 5))

原始答案 :
您可以使用带有逗号和 selectcolMeans
data.frame(x=4, y=6, z=3) %>%
  select(matches("x"), which(colMeans(.) > 5))
  x y
1 4 6

关于r - dplyr:同时按名称和值选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55595422/

10-11 23:04
查看更多