如何在dplyr中重命名反向包含的列名

时间:2017-04-14 14:40:06

标签: r dplyr

我有以下几点:

library(tidyverse)
df <- structure(list(`Input paired-end (STAR)` = c(9394981, 100), `Multi-mapped pair (STAR)` = c(1493691, 
400), `Uniquely mapped paired-end (STAR)` = c(6826405, 200), 
    `Unmapped pair (STAR-appx)` = c(1074885, 300)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = 1:2, .Names = c("Input paired-end (STAR)", 
"Multi-mapped pair (STAR)", "Uniquely mapped paired-end (STAR)", 
"Unmapped pair (STAR-appx)"))

看起来像这样:

# A tibble: 2 × 4
  `Input paired-end (STAR)` `Multi-mapped pair (STAR)` `Uniquely mapped paired-end (STAR)` `Unmapped pair (STAR-appx)`
*                     <dbl>                      <dbl>                               <dbl>                       <dbl>
1                   9394981                    1493691                             6826405                     1074885
2                       100                        400                                 200                         300

如何重命名列:

foo        bar    qux     gop
9394981 6826405 1074885 1493691 
100     200      300      400

2 个答案:

答案 0 :(得分:2)

一个简单的选项是setNames,也可以合并到链中

library(dplyr)
df %>%
     setNames(., c('foo', 'bar', 'qux', 'gop'))

答案 1 :(得分:2)

如果你真的想使用`dplyr函数,你可以使用重命名

df %>% rename(foo=`Input paired-end (STAR)`, 
  bar=`Multi-mapped pair (STAR)`,  
  qux=`Uniquely mapped paired-end (STAR)`,
  qop=`Unmapped pair (STAR-appx)`)

但使用基本功能

会更容易
names(df) <- c("foo","bar","qux","qop")