Data Science Desktop Survival Guide
by Graham Williams |
|||||
Introducing Template Variables |
20180721 A reference to the original dataset can be created using a template (or generic) variable. The new variable will be called ds (short for dataset).
# Prepare for a templated analysis and processing.
dsname <- "weatherAUS" ds <- get(dsname) ds %<>% clean_names(numerals="right") glimpse(ds)
We are a little tricky here in recording the dataset name in the variable dsname and then using the function base::get() to make a copy of the dataset reference and link it to the generic variable ds. We could simply assign the data to ds directly as we saw above. Either way the generic variable ds refers to the same dataset. The use of base::get() allows us to be a little more generic in our template. The use of generic variables within a template for the tasks we perform on each new dataset will have obvious advantages but we need to be careful. A disadvantage is that we may be working with several datasets and accidentally overwrite previously processed datasets referenced using the same generic variable (ds). The processing of the dataset might take some time and so accidentally losing it is not an attractive proposition. Care needs to be taken to avoid this.
|