Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go

Assignment Pipe

Raw 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 on the left onto the function on the right the result coming out 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.
# Copy the dataset into the variable ds.

ds <- weatherAUS

# Report on the dimensions of the dataset.

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

ds %<>%
  filter(Rainfall==0) %>%
  select(MinTemp, MaxTemp, Sunshine)

# Confirm that the dataset has changed.

dim(ds)
## [1] 112447      3

Once again this is so-called syntactic sugar. The commands are effectively translated by the computer into the following code.

# Functional form equivalent to the pipeline above.

ds <- select(filter(weatherAUS, Rainfall==0),
             MinTemp, MaxTemp, Sunshine)


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.