Bulk rename columns from one type of CCAO data to another. For example, rename all columns pulled from SQL to their standard names used in modeling. Or, rename all standard modeling names to "pretty" names for publication. This function will only rename things specified in the user-supplied dict argument, all other names in the data will remain unchanged.

Options for names_from and names_to are specific to the specified dict. Run this function with names_from equal to NULL to see a list of available options for the specified dictionary.

vars_rename(
  data,
  names_from = NULL,
  names_to = NULL,
  type = "inplace",
  dict = ccao::vars_dict
)

Arguments

data

A data frame or tibble with columns to be renamed.

names_from

The source/name type of data. See description

names_to

The target names. See description

type

Output type. Either "inplace", which renames the input data frame, or "vector", which returns a named character vector with the construction new_col_name = old_col_name.

dict

The dictionary used to translate names. Uses vars_dict by default. Use vars_dict_legacy for legacy data column names.

Value

The input data frame with columns renamed.

See also

Other vars_funs: vars_check_class(), vars_recode()

Examples


# Rename column names from SQL
sample_data <- chars_sample_universe[1:5, 18:27]
sample_data
#>   DT_PER_ASS  CDU DT_KEY_PIN APTS EXT_WALL ROOF_CNST ROOMS BEDS BSMT BSMT_FIN
#> 1          1 <NA>       <NA>    0        2         1     6    3    1        3
#> 2          1 <NA>       <NA>    0        2         1     6    3    1        3
#> 3          1   AV       <NA>    0        3         1     5    2    1        3
#> 4          1   AV       <NA>    0        2         1     6    2    1        3
#> 5          1 <NA>       <NA>    0        2         1     5    3    1        3

vars_rename(
  data = sample_data,
  names_from = "sql",
  names_to = "standard",
  dict = ccao::vars_dict_legacy
)
#>   meta_per_ass meta_cdu meta_key_pin char_apts char_ext_wall char_roof_cnst
#> 1            1     <NA>         <NA>         0             2              1
#> 2            1     <NA>         <NA>         0             2              1
#> 3            1       AV         <NA>         0             3              1
#> 4            1       AV         <NA>         0             2              1
#> 5            1     <NA>         <NA>         0             2              1
#>   char_rooms char_beds char_bsmt char_bsmt_fin
#> 1          6         3         1             3
#> 2          6         3         1             3
#> 3          5         2         1             3
#> 4          6         2         1             3
#> 5          5         3         1             3
vars_rename(
  data = sample_data,
  names_from = "sql",
  names_to = "pretty",
  dict = ccao::vars_dict_legacy
)
#>   Percent Assessed Condition, Desirability, and Utility Key PIN Apartments
#> 1                1                                 <NA>    <NA>          0
#> 2                1                                 <NA>    <NA>          0
#> 3                1                                   AV    <NA>          0
#> 4                1                                   AV    <NA>          0
#> 5                1                                 <NA>    <NA>          0
#>   Wall Material Roof Material Rooms Bedrooms Basement Basement Finish
#> 1             2             1     6        3        1               3
#> 2             2             1     6        3        1               3
#> 3             3             1     5        2        1               3
#> 4             2             1     6        2        1               3
#> 5             2             1     5        3        1               3

# No renames will occur since no column names here are from SQL
vars_rename(
  data = class_dict[1:5, 1:5],
  names_from = "sql",
  names_to = "pretty",
  dict = ccao::vars_dict_legacy
)
#> # A tibble: 5 × 5
#>   major_class_code major_class_type    assessment_level regression_class
#>   <chr>            <chr>               <chr>            <lgl>           
#> 1 0                Exempt and Railroad 0%               FALSE           
#> 2 0                Exempt and Railroad 0%               FALSE           
#> 3 1                Vacant              10%              FALSE           
#> 4 1                Vacant              10%              FALSE           
#> 5 2                Residential         10%              FALSE           
#> # ℹ 1 more variable: modeling_group <chr>

# With data from Athena
sample_data_athena <- chars_sample_athena[1:5, 1:10]
sample_data_athena
#>              pin year class char_yrblt char_bldg_sf char_land_sf char_beds
#> 1 10254170360000 2015   205       1948         1775         4340         4
#> 2 09363230550000 2019   203       1923         1200         4375         3
#> 3 09363230550000 2016   203       1923         1200         4375         3
#> 4 14321260280000 2018   211       1878         2850         3125         4
#> 5 10253190450000 2018   204       1951         2469        11160         3
#>   char_rooms char_fbath char_hbath
#> 1          7          2          1
#> 2          5          1          1
#> 3          5          1          1
#> 4          9          3          0
#> 5          8          2          1

vars_rename(
  data = sample_data_athena,
  names_from = "athena",
  names_to = "model",
  dict = ccao::vars_dict
)
#>         meta_pin meta_year meta_class char_yrblt char_bldg_sf char_land_sf
#> 1 10254170360000      2015        205       1948         1775         4340
#> 2 09363230550000      2019        203       1923         1200         4375
#> 3 09363230550000      2016        203       1923         1200         4375
#> 4 14321260280000      2018        211       1878         2850         3125
#> 5 10253190450000      2018        204       1951         2469        11160
#>   char_beds char_rooms char_fbath char_hbath
#> 1         4          7          2          1
#> 2         3          5          1          1
#> 3         3          5          1          1
#> 4         4          9          3          0
#> 5         3          8          2          1
vars_rename(
  data = sample_data_athena,
  names_from = "athena",
  names_to = "pretty",
  dict = ccao::vars_dict
)
#>              PIN Year Property Class Year Built Building Square Feet
#> 1 10254170360000 2015            205       1948                 1775
#> 2 09363230550000 2019            203       1923                 1200
#> 3 09363230550000 2016            203       1923                 1200
#> 4 14321260280000 2018            211       1878                 2850
#> 5 10253190450000 2018            204       1951                 2469
#>   Land Square Feet Bedrooms Rooms Full Baths Half Baths
#> 1             4340        4     7          2          1
#> 2             4375        3     5          1          1
#> 3             4375        3     5          1          1
#> 4             3125        4     9          3          0
#> 5            11160        3     8          2          1