3.13 Pipes: Assignment Pipe
20210103 The are several variations of the pipe operator
available. A particularly handy operator implemented by
magrittr (Bache and Wickham 2026) 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 dplyr::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.
## [1] 275410 24
# Demonstrate an assignment pipeline.
ds %<>%
filter(rainfall==0) %>%
select(min_temp, max_temp, sunshine)
# Confirm that the dataset has changed.
dim(ds)## [1] 171900 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).
References
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