使用dplyr有条件地替换tibble中的列名

时间:2017-04-24 02:07:58

标签: r dplyr tidyverse

我有以下几点:

    df <- structure(list(gene_symbol = c("0610005C13Rik", "0610007P14Rik", 
"0610009B22Rik", "0610009L18Rik", "0610009O20Rik", "0610010B08Rik"
), foo.control.cv = c(1.16204038288333, 0.120508045270669, 0.205712615954009, 
0.504508040948641, 0.333956330117591, 0.543693011377001), foo.control.mean = c(2.66407458486012, 
187.137728870855, 142.111269303428, 16.7278587043453, 69.8602872478098, 
4.77769028710622), foo.treated.cv = c(0.905769898934564, 0.186441944401973, 
0.158552512842753, 0.551955061149896, 0.15743983656006, 0.290447431974039
), foo.treated.mean = c(2.40658723367692, 180.846795140269, 139.054032348287, 
11.8584348984435, 76.8141734599118, 2.24088124240385)), .Names = c("gene_symbol", 
"foo.control.cv", "foo.control.mean", "foo.treated.cv", "foo.treated.mean"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
6L))

看起来像这样:

# A tibble: 6 × 5
    gene_symbol foo.control.cv foo.control.mean foo.treated.cv foo.treated.mean
*         <chr>          <dbl>            <dbl>          <dbl>            <dbl>
1 0610005C13Rik      1.1620404         2.664075      0.9057699         2.406587
2 0610007P14Rik      0.1205080       187.137729      0.1864419       180.846795
3 0610009B22Rik      0.2057126       142.111269      0.1585525       139.054032
4 0610009L18Rik      0.5045080        16.727859      0.5519551        11.858435
5 0610009O20Rik      0.3339563        69.860287      0.1574398        76.814173
6 0610010B08Rik      0.5436930         4.777690      0.2904474         2.240881

我想要做的是将mean中的所有列名替换为mean_expr。导致

    gene_symbol foo.control.cv foo.control.mean_expr foo.treated.cv foo.treated.mean_expr

1 0610005C13Rik      1.1620404         2.664075      0.9057699         2.406587
2 0610007P14Rik      0.1205080       187.137729      0.1864419       180.846795
3 0610009B22Rik      0.2057126       142.111269      0.1585525       139.054032
4 0610009L18Rik      0.5045080        16.727859      0.5519551        11.858435
5 0610009O20Rik      0.3339563        69.860287      0.1574398        76.814173
6 0610010B08Rik      0.5436930         4.777690      0.2904474         2.240881

我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:23)

使用当前版本的dplyr,您可以使用rename_at

library(dplyr)

df %>% rename_at(vars(contains('mean')), funs(sub('mean', 'mean_expr', .)))
#> # A tibble: 6 × 5
#>     gene_symbol foo.control.cv foo.control.mean_expr foo.treated.cv
#> *         <chr>          <dbl>                 <dbl>          <dbl>
#> 1 0610005C13Rik      1.1620404              2.664075      0.9057699
#> 2 0610007P14Rik      0.1205080            187.137729      0.1864419
#> 3 0610009B22Rik      0.2057126            142.111269      0.1585525
#> 4 0610009L18Rik      0.5045080             16.727859      0.5519551
#> 5 0610009O20Rik      0.3339563             69.860287      0.1574398
#> 6 0610010B08Rik      0.5436930              4.777690      0.2904474
#> # ... with 1 more variables: foo.treated.mean_expr <dbl>

实际上,您也可以使用rename_all,因为不匹配的名称无论如何都不会受到影响。此外,您可以使用rlang::as_function .funs来强制执行某个函数的quosure或任何函数,因此您可以使用purrr样式表示法:

df %>% rename_all(~sub('mean', 'mean_expr', .x))

由于数据框是一个列表,Movies of the Year jekyll github pageset_names可以做同样的事情:

library(purrr)    # or library(tidyverse)

df %>% set_names(~sub('mean', 'mean_expr', .x))

所有回报都是一样的。

答案 1 :(得分:5)

另一个选项是onclick中的paste(使用devel版本的dplyr)

rename_at

或使用library(dplyr) df %>% rename_at(vars(matches('mean')), funs(sprintf('%s_expr', .))) # A tibble: 6 × 5 # gene_symbol foo.control.cv foo.control.mean_expr foo.treated.cv foo.treated.mean_expr #* <chr> <dbl> <dbl> <dbl> <dbl> #1 0610005C13Rik 1.1620404 2.664075 0.9057699 2.406587 #2 0610007P14Rik 0.1205080 187.137729 0.1864419 180.846795 #3 0610009B22Rik 0.2057126 142.111269 0.1585525 139.054032 #4 0610009L18Rik 0.5045080 16.727859 0.5519551 11.858435 #5 0610009O20Rik 0.3339563 69.860287 0.1574398 76.814173 #6 0610010B08Rik 0.5436930 4.777690 0.2904474 2.240881

rename_if

答案 2 :(得分:3)

这是一个非dplyr基R方法:

names(df) <- sub("mean$", "mean_expr", names(df))
# or names(df) <- sub("mean", "mean_expr", names(df)) if the mean doesn't have to be at the 
# end of the string

names(df)
#[1] "gene_symbol"           "foo.control.cv"        "foo.control.mean_expr"
#[4] "foo.treated.cv"        "foo.treated.mean_expr"

如果您希望它成为管道的一部分,您可以使用 setNames 功能:

df %>% setNames(sub("mean", "mean_expr", names(.))) %>% names(.)
#[1] "gene_symbol"           "foo.control.cv"        "foo.control.mean_expr"
#[4] "foo.treated.cv"        "foo.treated.mean_expr"

答案 3 :(得分:2)

另一个选项是dplyr::select_all()

df %>% select_all(~gsub("mean", "mean_expr", .))

答案 4 :(得分:0)

通过使用magritrr,您可以拥有

library(magrittr)
names(df)[df %>% names %>% grep(pattern = "mean")] %<>% paste0("_expr")
df
# A tibble: 6 x 5
  gene_symbol   foo.control.cv foo.control.mean_expr foo.treated.cv foo.treated.mean_expr
* <chr>                  <dbl>                 <dbl>          <dbl>                 <dbl>
1 0610005C13Rik          1.16                   2.66          0.906                  2.41
2 0610007P14Rik          0.121                187.            0.186                181.  
3 0610009B22Rik          0.206                142.            0.159                139.  
4 0610009L18Rik          0.505                 16.7           0.552                 11.9 
5 0610009O20Rik          0.334                 69.9           0.157                 76.8 
6 0610010B08Rik          0.544                  4.78          0.290                  2.24