在dplyr :: filter_at中使用`tidyselect`选择特定的列

时间:2019-11-22 12:06:11

标签: r dplyr tidyverse tidyselect

除了可以选择meaningful列之外,这可以按预期工作。我只希望选择以下列:

mean...summarymean.conf.low...summarymean.conf.high...summary

我该怎么做?

library(tidyverse)

# dataframe
df <- structure(
  list(
    group = structure(1:2, .Label = c("a", "b"), class = "factor"),
    meaningful = c(0.98, 1.39333333333333),
    mean...summary = c(0.98,
                       1.39333333333333),
    n...summary = c(3L, 3L),
    mean.conf.low...summary = c(0.717103575690863,
                                0.921129311562406),
    mean.conf.high...summary = c(1.24289642430914,
                                 1.86553735510426)
  ),
  class = c("tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -2L)
)

# changing few columns
df %>%
  dplyr::mutate_at(
    .tbl = .,
    .vars = dplyr::vars(dplyr::matches("^mean...|^mean.conf")),
    .funs = ~ format(round(x = ., digits = 3), nsmall = 3)
  )
#> # A tibble: 2 x 6
#>   group meaningful mean...summary n...summary mean.conf.low...~ mean.conf.high.~
#>   <fct> <chr>      <chr>                <int> <chr>             <chr>           
#> 1 a     0.980      0.980                    3 0.717             1.243           
#> 2 b     1.393      1.393                    3 0.921             1.866

reprex package(v0.3.0)于2019-11-22创建

1 个答案:

答案 0 :(得分:3)

.在正则表达式中有特殊含义,因此您可以使用.将其跳过并视为文字\\,例如 matches("^mean\\.\\.\\.|^mean\\.conf")matches("^mean\\.{3}|^mean\\.conf")