3.2 Assignment

20210103 The assignment operator in R base::<- is used to save a result into the computer’s memory and give it a name that we can refer to later. We can use this operator to save the result of the sequence of operations from a pipeline (see Section 3.7).

# Select variables/observations and save the result.

rainy_days <-
  ds %>% 
  select(min_temp, max_temp, rainfall, sunshine) %>%
  filter(rainfall >= 1)

An alternative that makes sense within the pipe paradigm that we use is to use the forward assignment operator base::-> to save the resulting data into a variable in a logically forward flowing way.

# Demonstrate use of the forward assignment operator.

ds %>% 
  select(min_temp, max_temp, rainfall, sunshine) %>%
  filter(rainfall >= 1) ->
  rainy_days

Using the forward assignment operator tends to hide the final variable into which the assignment is made. This important side effect of the series of commands, the assignment to rainy_days, could easily be missed unlike our use of the backward assignment earlier.

When using the forward assignment be sure to un-indent the final variable name so that the assignment is clearer and the un-indented variable name serves as the final bracket for the command pipeline.

# Best to unindent the final assigned variable for clarity.

ds %>% 
  select(min_temp, max_temp, rainfall, sunshine) %>%
  filter(rainfall >= 1) ->
rainy_days


Your donation will support ongoing development 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-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.