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

Pipes: Assignment Pipe

20210103 The are several variations of the pipe operator available. A particularly handy operator implemented by magrittr is the assignment pipe magrittr::https://www.rdocumentation.org/packages/magrittr/topics/left most pipe of any sequence of pipes. In addition to piping the dataset named on the left onto the function on the right the result coming out at the end of the right-hand pipeline is piped back to the original dataset overwriting its original contents in memory with the results from the pipeline. A simple example is to replace a dataset with the same dataset after removing some observations (rows) and variables (columns). In the example below we stats::filter() and dplyr::select() the dataset to reduce it to just those observations and variables of interest. The result is piped backwards to the original dataset and thus overwrites the original data (which may or may not be a good thing). We do this on a temporary copy of the dataset and use the base::dim() function to report on the dimensions (rows and columns) of the resulting datasets.
ods <- ds

# Report on the dimensions of the dataset.

dim(ds)
## [1] 176747     24
# Demonstrate an assignment pipeline.

ds %<>%
  filter(rainfall==0) %>%
  select(min_temp, max_temp, sunshine)

# Confirm that the dataset has changed.

dim(ds)
## [1] 112447      3

Once again this is so-called syntactic sugar. The core assignment command is effectively translated by the computer into the following code (noting we did a little more that just the assignment in the above cod block).

# Functional form equivalent to the pipeline above.

ds <- select(filter(ds, rainfall==0), min_temp, max_temp, sunshine)

ds <- ods


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.