简体   繁体   中英

How to rename backticked contained column names in dplyr

I have the following tibble:

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)"))

That looks like this:

# 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

How can I rename the column too:

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

An easy option is with setNames which can also be incorporated into the chain

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

If you really wanted to use `dplyr functions, you would use rename

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

But it would be much easier with base functions

names(df) <- c("foo","bar","qux","qop")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM