Selecting columns with dplyr
In the last recipe, we introduced how to use the filter
and slice
functions to subset and slice data by rows. In this recipe, we will present how to select particular columns from the dataset using the select
function.
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 select columns from the dataset:
First, let's select the
Quantity
andPrice
columns from the dataset:> select.quantity.price <- select(order.dt, Quantity, Price) > head(select.quantity.price, 3) Quantity Price 1: 1 1069 2: 1 1680 3: 1 285
Alternatively, we can rule out the
Price
column by placing a minus sign in front:> select.not.price <- select(order.dt...