Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to Count Distinct Values in R
    How to Count Distinct Values in R R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • pheatmap function in R
    The pheatmap function in R R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • How to Filter Rows In R
    How to Filter Rows In R? R
  • Extract patterns in R
    Extract patterns in R? R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • How to create contingency tables in R
    How to create contingency tables in R? 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 Jim No Comments on Arrange the rows in a specific sequence in R
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • best books about data analytics
    Best Books to learn Tensorflow Course
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • best books about data analytics
    Best Books About Data Analytics Course
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme