10.4 Add Columns Using Variables
20200814 New columns can be added to a dataset where the new names are generated or supplied as the value of a variable. The example below uses uses the non-standard evaluation constructs and is generally useful when defining functions and the new variable name is passed through the function call. The notation includes the double exclamations (bang bang) which, technically, unquotes the following variable to replace it with its actual value. The colon equals is required in order to support unquoting the left hand side.
newvar <- "mid_temp"
ds %>%
mutate( newvar = (max_temp + min_temp)/2,
!!newvar := (max_temp + min_temp)/2) %T>%
{
select(., date, location, ends_with("_temp"), newvar) %>%
sample_frac() %>%
print()
} ->
newds
## # A tibble: 226,868 × 6
## date location min_temp max_temp mid_temp newvar
## <date> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 2014-02-25 Ballarat 14.1 32.1 23.1 23.1
## 2 2010-08-02 Witchcliffe 3.5 20.8 12.2 12.2
## 3 2022-12-03 Melbourne 13.6 31 22.3 22.3
## 4 2012-11-01 GoldCoast 17.3 27 22.2 22.2
## 5 2017-07-06 Mildura 1.6 17.8 9.7 9.7
## 6 2022-04-24 Brisbane 16.5 23.9 20.2 20.2
## 7 2012-08-15 Penrith 3.7 24.4 14.0 14.0
## 8 2009-01-06 Watsonia 12.8 31.7 22.2 22.2
## 9 2021-11-10 Perth 11.4 21.4 16.4 16.4
## 10 2017-06-08 Bendigo -1 14 6.5 6.5
## # ℹ 226,858 more rows
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0