Hank Lin
Hank Lin

Reputation: 6509

Renaming columns in a dataframe based on a vector

I was able to do this, but was wondering if there was a more elegant way, possibly with dplyr rename?

    # Create dataframe with three named columns
    tb <- tibble(col1 = 1:3, col2 = 1:3, col3 = 1:3)
    #> # A tibble: 3 x 3
    #>    col1  col2  col3
    #>   <int> <int> <int>
    #> 1     1     1     1
    #> 2     2     2     2
    #> 3     3     3     3


    # Named vector with replacement names
    new_names <- c(col1 = "Column 1", col3 = "Col3")
    #>       col1       col3 
    #> "Column 1"     "Col3"

    # Rename columns within dataframe
    tb <- new_names[colnames(tb)] %>% 
      coalesce(colnames(tb)) %>% 
      setNames(object = tb, nm = .)
    #> # A tibble: 3 x 3
    #>   `Column 1`  col2  Col3
    #>        <int> <int> <int>
    #> 1          1     1     1
    #> 2          2     2     2
    #> 3          3     3     3

Upvotes: 0

Views: 78

Answers (1)

Damian Fiłonowicz
Damian Fiłonowicz

Reputation: 46

# loading dplyr
pacman::p_load(dplyr)

# rename() syntax demands:
# LHS - a new column name
# RHS - an existing column name

# can be either a named vector or a named list
c('Column 1' = 'col1', 'Col3' = 'col3') -> x

# the unquote-splice (!!!) operator unquotes and splices its argument
rename(tibble(col1 = 1:3, col2 = 1:3, col3 = 1:3), !!!x)
#> # A tibble: 3 x 3
#>   `Column 1`  col2  Col3
#>        <int> <int> <int>
#> 1          1     1     1
#> 2          2     2     2
#> 3          3     3     3

You can find more about it here: a good book

And here: pretty documentation

Pipe operators are kinda slow so you ought to try to avoid using them when not needed.

Upvotes: 1

Related Questions