Arranging rows with dplyr
Arranging rows in order may help us rank data by value or gain a more structured view of data in the same category. In this recipe, we will introduce how to arrange rows with 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 arrange data with dplyr
:
To arrange data by price, pass
Price
to thearrange
function:> order.dt %>% arrange(Price) %>% head(3) Time Action User Product Quantity Price 1: 2015-07-30 23:54:01 order U166076125 P0003659246 3 10 2: 2015-07-03 09:19:24 order U1012162712 P0009305785 1 15 3: 2015-07-07 23:45:09 order U423898356 P0023454992 1 15
We can also arrange rows by
price
in descending order:> order.dt %>% arrange(desc(Price)) %>% head(3) Time Action...