Skip to content

Data Science Tutorials

For Data Science Learners

  • How to remove files and folders in R
    How to remove files and folders in R R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • Autocorrelation and Partial Autocorrelation in Time Series
    Autocorrelation and Partial Autocorrelation in Time Series Statistics
  • Group By Maximum in R
    Group By Maximum in R R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • Tips for Data Scientist Interview Openings
    Tips for Data Scientist Interview Openings Course
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
Arrange the rows in a specific sequence in R

Arrange the rows in a specific sequence in R

Posted on June 2June 2 By Admin No Comments on Arrange the rows in a specific sequence in R

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

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Arrange Data by Month in R with example
Next Post: Remove Rows from the data frame in R

Related Posts

  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • How to remove files and folders in R
    How to remove files and folders in R R
  • Extract columns of data frame in R R
  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
  • Extract values from vector in R: dplyr R
  • How to Display Percentages on Histogram IN R
    How to Display Percentages on Histogram in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • How to Standardize Data in R
    How to Standardize Data in R? R
  • How to use image function in R
    How to use the image function in R R
  • How to Check if a Directory Exists in R
    How to Check if a Directory Exists in R R
  • Ogive Graph in R
    Ogive Graph in R R
  • Jarque-Bera Test in R
    Jarque-Bera Test in R With Examples R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme