Subsetting and slicing data with dplyr
In this recipe, we will introduce how to use dplyr
to manipulate data. We first cover the topic of how to use the filter
and slice
functions to subset and slice data.
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.
You also need to make sure that you have a version of R higher than 3.1.2 installed on your operating system.
How to do it…
Perform the following steps to subset and slice data with dplyr
:
Let's first install and load the
dplyr
package:> install.packages("dplyr") > library(dplyr)
Next, we can filter data by quantity number with the
filter
function:> quantity.over.3 <- filter(order.dt, Quantity >= 3) > head(quantity.over.3, 3) Time Action User Product Quantity Price 1: 2015-07-01 00:39:22 order U465146448 P0006173160 3 1076 2: 2015-07-01 00...