3.12 Pipes: Assignment Pipe
20210103 The are several variations of the pipe operator
available. A particularly handy operator implemented by
magrittr (Bache and Wickham 2020) is the assignment pipe
%<>%. This operator
should be the 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.
# Report on the dimensions of the dataset. dim(ds)
##  191431 24
# Demonstrate an assignment pipeline. %<>% ds filter(rainfall==0) %>% select(min_temp, max_temp, sunshine) # Confirm that the dataset has changed. dim(ds)
##  121167 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. <- select(filter(ds, rainfall==0), min_temp, max_temp, sunshine)ds
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.