GaryZA73
GaryZA73

Reputation: 67

A quick way to rename multiple columns with unique names using dplyr

I am beginner R user, currently learning the tidyverse way. I imported a dataset which is a time series of monthly indexed consumer prices over a period of four years. The imported headings on the monthly CPI columns displayed in R as five digit numbers (as characters). Here is a short mockup recreation of what it looks like...

df <- tibble(`Product` = c("Eggs", "Chicken"),
              `44213` = c(35.77, 36.77),
              `44244` = c(39.19, 39.80),
              `44272` = c(40.12, 43.42),
              `44303` = c(41.09, 41.33)
)

# A tibble: 2 x 5
#  Product `44213` `44244` `44272` `44303`
#  <chr>     <dbl>   <dbl>   <dbl>   <dbl>
#1 Eggs       35.8    39.2    40.1    41.1
#2 Chicken    36.8    39.8    43.4    41.3

I want to change the column headings (44213 etc) to dates that make more sense to me (still as characters). I understand, using dplyr, to do it the following way:

df <- df %>% rename("Jan17" = `44213`, "Feb17" = `44244`, 
                    "Mar17" = `44272`, "Apr17" = `44303`)

# A tibble: 2 x 5
#  Product Jan17 Feb17 Mar17 Apr17
#  <chr>   <dbl> <dbl> <dbl> <dbl>
#1 Eggs     35.8  39.2  40.1  41.1
#2 Chicken  36.8  39.8  43.4  41.3

The problem is that my actual dataset contains 48 such columns (months) to rename and so it is a lot of work to type out. I looked at other replace and set_names functions but these seem to add in the repeated changes to the column names, don't provide new unique names like I am looking for?

(I realise dates as columns is not good practice and would need to shift these to rows before proceeding with any analysis... or maybe this must be a prior step to renaming?)

Trust I expressed my question sufficiently. Would love to learn a quicker solution using dplyr or be directed to where one can be found. Thank you for your time.

Upvotes: 1

Views: 543

Answers (2)

Sathish
Sathish

Reputation: 12703

using some random names, but sequentially

names(df)[2:ncol(df)] <- paste0('col_', 1:(ncol(df)-1), sep = '')

## A tibble: 2 x 5
#  Product col_1 col_2 col_3 col_4
#  <chr>   <dbl> <dbl> <dbl> <dbl>
#1 Eggs     35.8  39.2  40.1  41.1
#2 Chicken  36.8  39.8  43.4  41.3

Upvotes: 1

akrun
akrun

Reputation: 886938

We can use !!! with rename by passing a named vector

library(dplyr)
library(stringr)
df1 <- df %>% 
     rename(!!! setNames(names(df)[-1], str_c(month.abb[1:4], 17)))

-output

df1
# A tibble: 2 x 5
#  Product Jan17 Feb17 Mar17 Apr17
#  <chr>   <dbl> <dbl> <dbl> <dbl>
#1 Eggs     35.8  39.2  40.1  41.1
#2 Chicken  36.8  39.8  43.4  41.3

Or use rename_with

df %>% 
     rename_with(~str_c(month.abb[1:4], 17), -1)

If the column names should be converted to Date formatted

nm1 <- format(as.Date(as.numeric(names(df)[-1]), origin = '1896-01-01'), '%b%y')

df %>%
    rename_with(~ nm1, -1)
# A tibble: 2 x 5
#  Product Jan17 Feb17 Mar17 Apr17
#  <chr>   <dbl> <dbl> <dbl> <dbl>
#1 Eggs     35.8  39.2  40.1  41.1
#2 Chicken  36.8  39.8  43.4  41.3

Upvotes: 1

Related Questions