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:

  1. To arrange data by price, pass Price to the arrange 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 ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.