Chaining operations in dplyr
To perform multiple operations on data using dplyr
, we can wrap up the function calls into a larger function call. Or, we can use the %>%
chaining operator to chain operations instead. In this recipe, we introduced how to chain operations when using dplyr
.
Getting ready
Ensure that you completed the Enhancing a data.frame with a data.table recipe to load purchase_view.tab
and purchase_order.tab
as both data.frame
and data.table
into your R environment.
How to do it…
Perform the following steps to subset and slice data with dplyr
:
In R, to sum up a sequence from
1
to10
, we can wrap the series of1
to10
with thesum
function:> sum(1:10) [1] 55
Alternatively, we can use a chaining operator to chain operations:
> 1:10 %>% sum() [1] 55
To select and filter data with
dplyr
, we can wrap the filtered data in aselect
function:> select.p.price.over.1000 <- select(filter(order.dt, Price >= 1000 ), contains('P') ) > head(select.p.price.over.1000,...