3.3 Assignment

20220523 The assignment operator in R r Rfunction('<-',show.paren=FALSE) 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.8).

# Select variables/observations and save the result.

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

Within the pipe paradigm we can 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.

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

An intermediate value can also be assigned in the midst of a pipeline using base::assign():

ds %>% 
  select(min_temp, max_temp, rainfall, sunshine) %T>%
  assign("intermediate", ., globalenv()) %>%
  filter(rainfall >= 1) ->
rainy_days

A side track processing of the data can also be assigned as an intermediate:

ds %>% 
  select(min_temp, max_temp, rainfall, sunshine) %T>%
  {assign("all_days_rain", . %>% select(rainfall), globalenv()) %>%
  filter(rainfall >= 1) ->
rainy_days


If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, 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