Data Science Desktop Survival Guide
by Graham Williams |
|||||
Pipeline Syntactic Sugar |
# Summarise observations with little or no rainfall.
ds %>% select(min_temp, max_temp, rainfall, sunshine) %>% filter(rainfall < 1) %>% summary()
Contrast this with how it is mapped by R into the functional construct below, which is how we might have traditionally written it. For many of us it will take quite a bit of effort to parse this traditional functional form of the expression, and so to understand what it is doing. The pipeline alternative above provides a clearer narrative. |
# Functional form equivalent to the pipeline above.
summary(filter(select(ds, min_temp, max_temp, rainfall, sunshine), rainfall < 1))
Anything that improves the readability of our code is useful. Computers are quite capable of doing the hard work of transforming a simpler sentence into this much more complex looking sentence for its own purposes. For our purposes, let's keep it simple for others to follow. |