Dal
Dal

Reputation: 317

Rename Column Data

I have a list of observations in one column that I'd like to rename. The dataframe is shown below. The column GEO has 40 distinct names that I'd like to rename. I've created another list with 40 alternative names that I'd like to use. Though I have thousands of observations, I only have 40 distinct names in there.

DataFrame

**Code to Reproduce**

library(cansim)
library(dplyr)

HPI_LIST <- c("v111955442", "v111955445",   "v111955448",   "v111955451",   "v111955454",   "v111955457",   "v111955460", "v111955463", "v111955466",   "v111955469",   "v111955472",   "v111955475",   "v111955478",   "v111955481",   "v111955484",   "v111955487",   "v111955490",   "v111955493",   "v111955496",   "v111955499",   "v111955502",   "v111955505",   "v111955508",   "v111955511",   "v111955514",   "v111955517",   "v111955520",   "v111955523",   "v111955526",   "v111955529",   "v111955532",   "v111955535",   "v111955538",   "v111955541",   "v111955544",   "v111955547",   "v111955550",   "v111955553",   "v111955556",   "v111955559")

NewHPIData <- get_cansim("18-10-0205-01")

NewHPI <- NewHPIData %>% filter(VECTOR %in% HPI_LIST, REF_DATE > 2018) %>% 
          arrange((COORDINATE)) %>% 
          select(REF_DATE, GEO, VALUE) %>% mutate(VALUE = VALUE / 100) %>%
          arrange(GEO)

I'd like to update names in GEO for example I'd like to rename Alberta with "New Housing Price Index - AB (x 100)"

Upvotes: 2

Views: 322

Answers (1)

Artem Sokolov
Artem Sokolov

Reputation: 13691

Define your name map as a list

nameMap <- list( "Alberta" = "New Housing Price Index - AB (x 100)",
                "Atlantic Region" = "New Name 1",
                "British Columbia" = "New Name 2"
                ## and so on )

Then call recode on your GEO column using mutate_at:

NewHPI %>% mutate_at( "GEO", recode, !!!nameMap )
# # A tibble: 320 x 3
#    REF_DATE GEO                                  VALUE
#    <chr>    <chr>                                <dbl>
#  1 2018-01  New Housing Price Index - AB (x 100) 1.00 
#  2 2018-02  New Housing Price Index - AB (x 100) 0.998
#  3 2018-03  New Housing Price Index - AB (x 100) 0.996
#  4 2018-04  New Housing Price Index - AB (x 100) 0.996
#  5 2018-05  New Housing Price Index - AB (x 100) 0.996
#  6 2018-06  New Housing Price Index - AB (x 100) 0.998
#  7 2018-07  New Housing Price Index - AB (x 100) 0.999
#  8 2018-08  New Housing Price Index - AB (x 100) 0.994
#  9 2018-01  New Name 1                           1.00 
# 10 2018-02  New Name 1                           1.00 

The !!! operator is needed, because recode accepts an arbitrary number of arguments using ..., but you have a predefined list. This mechanism is known as unquote-splicing.

Upvotes: 1

Related Questions