Arrange the rows in a specific sequence in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Arrange the rows in a specific sequence in R appeared first on Data Science Tutorials

Arrange the rows in a specific sequence in R, Frequently, you’ll want to arrange the rows in a data frame in R in a specified order.

Fortunately, the arrange() function from the dplyr library makes this simple.

Using the data frame below, this tutorial shows numerous examples of how to utilize this function in practice.

Arrange Data by Month in R with example – Data Science Tutorials

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'),
                 points = c(122, 144, 154, 155, 120, 218, 229),
                 assists = c(43, 55, 77, 18, 114, NA,29))

Now we can view the data frame

df
    player points assists
1     P1    122      43
2     P2    144      55
3     P3    154      77
4     P4    155      18
5     P5    120     114
6     P6    218      NA
7     P7    229      29

Approach 1: Organize by one Column

The following code demonstrates how to ascend the data frame using the values in the ‘points’ column.

Get the first value in each group in R? – Data Science Tutorials

library(dplyr)
df %>% arrange(points)
  player points assists
1     P5    120     114
2     P1    122      43
3     P2    144      55
4     P3    154      77
5     P4    155      18
6     P6    218      NA
7     P7    229      29

The desc() function can be used to sort in descending order:

df %>% arrange(desc(points))
   player points assists
1     P7    229      29
2     P6    218      NA
3     P4    155      18
4     P3    154      77
5     P2    144      55
6     P1    122      43
7     P5    120     114

Note that whether you sort ascending or descending, NA’s will be at the bottom.

Subsetting with multiple conditions in R – Data Science Tutorials

df %>% arrange(assists)
player points assists
1     P4    155      18
2     P7    229      29
3     P1    122      43
4     P2    144      55
5     P3    154      77
6     P5    120     114
7     P6    218      NA
df %>% arrange(desc(assists))
   player points assists
1     P5    120     114
2     P3    154      77
3     P2    144      55
4     P1    122      43
5     P7    229      29
6     P4    155      18
7     P6    218      NA

Approach 2: Arrange by Multiple Columns

We can simply provide extra column names as parameters to organize the rows by multiple columns.

Rejection Region in Hypothesis Testing – Data Science Tutorials

order by points, then by assistance

df %>% arrange(points, assists)
   player points assists
1     P5    120     114
2     P1    122      43
3     P2    144      55
4     P3    154      77
5     P4    155      18
6     P6    218      NA
7     P7    229      29

We can also arrange the rows by ascending one column and descending the other.

Sort by ascending points, then by descending assists.

Similarity Measure Between Two Populations-Brunner Munzel Test – Data Science Tutorials

df %>% arrange(points, desc(assists))
   player points assists
1     P5    120     114
2     P1    122      43
3     P2    144      55
4     P3    154      77
5     P4    155      18
6     P6    218      NA
7     P7    229      29

Example 3: Arrange the rows in a specific sequence in R

You could also want to sort the rows in a specific order on occasion. This is simple to perform if you use a factor with precise levels.

Methods for Integrating R and Hadoop complete Guide – Data Science Tutorials

sort by a player in a specific order

df %>% arrange(factor(player, levels = c('P1', 'P3', 'P2', 'P5', 'P4', 'P6', 'P7')))
   player points assists
1     P1    122      43
2     P3    154      77
3     P2    144      55
4     P5    120     114
5     P4    155      18
6     P6    218      NA
7     P7    229      29

The post Arrange the rows in a specific sequence in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)