5.7 Piping Operations

R users are familiar with the pipe operator %>% which allows chaining operations together. That means that the output of an operation will be used as input in the next operation and so on.

This can be accomplished with the @chain macro. To use it, we start with @chain followed by the DataFrame and a begin statement. Every operation inside the begin ... end statement will be used as input for the next operation, therefore chaining operations together.

Here is a simple example with a groupby followed by a @combine:

  1. @chain leftjoined begin
  2. groupby(:name)
  3. @combine :mean_grade_2020 = mean(:grade_2020)
  4. end
namemean_grade_2020
Sally1.0
Hank4.0
Bob5.0
Alice8.5

NOTE: @chain will replace the first positional argument while chaining operations. This is not a problem in DataFrames.jl and DataFramesMeta.jl, since the DataFrame is always the first positional argument.

We can also nest as many as begin ... end statements we desired inside the operations:

  1. @chain leftjoined begin
  2. groupby(:name)
  3. @combine begin
  4. :mean_grade_2020 = mean(:grade_2020)
  5. :mean_grade_2021 = mean(:grade_2021)
  6. end
  7. end
namemean_grade_2020mean_grade_2021
Sally1.09.5
Hank4.06.0
Bob5.05.0
Alice8.55.0

To conclude, let’s show a @chain example with all of the DataFramesMeta.jl macros we covered so far:

  1. @chain leftjoined begin
  2. @rtransform begin
  3. :grade_2020 = :grade_2020 * 10
  4. :grade_2021 = :grade_2021 * 10
  5. end
  6. groupby(:name)
  7. @combine begin
  8. :mean_grade_2020 = mean(:grade_2020)
  9. :mean_grade_2021 = mean(:grade_2021)
  10. end
  11. @rtransform :mean_grades = (:mean_grade_2020 + :mean_grade_2021) / 2
  12. @rsubset :mean_grades > 50
  13. @orderby -:mean_grades
  14. end
namemean_grade_2020mean_grade_2021mean_grades
Alice85.050.067.5
Sally10.095.052.5

5.7 Piping Operations - 图1 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso