Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Normalise Variable Names

Variable names are normalised so as to have some certainty in interacting with the data. The convenience function rattle::normVarNames() can do this.

# Review the variables before normalising their names.

names(ds)
##  [1] "Date"          "Location"      "MinTemp"       "MaxTemp"      
##  [5] "Rainfall"      "Evaporation"   "Sunshine"      "WindGustDir"  
##  [9] "WindGustSpeed" "WindDir9am"    "WindDir3pm"    "WindSpeed9am" 
## [13] "WindSpeed3pm"  "Humidity9am"   "Humidity3pm"   "Pressure9am"  
....

# Capture the original variable names for use in plots.

vnames <- names(ds)

# Normalise the variable names.

ds %<>% rename_all(normVarNames)

# Confirm the results are as expected.

names(ds)
##  [1] "date"            "location"        "min_temp"        "max_temp"   ...
##  [5] "rainfall"        "evaporation"     "sunshine"        "wind_gust_di...
##  [9] "wind_gust_speed" "wind_dir_9am"    "wind_dir_3pm"    "wind_speed_9...
## [13] "wind_speed_3pm"  "humidity_9am"    "humidity_3pm"    "pressure_9am...
....

# Index the original variable names by the new names.

names(vnames) <- names(ds)

vnames
##            date        location        min_temp        max_temp 
##          "Date"      "Location"       "MinTemp"       "MaxTemp" 
##        rainfall     evaporation        sunshine   wind_gust_dir 
##      "Rainfall"   "Evaporation"      "Sunshine"   "WindGustDir" 
....

Notice that we capture the original variable names in the variable []vnames for reference, and particularly when generating plots and wanting to use the original names.

The variable names now conform to our expectations of them and in accordance to our chosen style as documented in Chapter 23.


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.