Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Contact
  • About Us
  • Toggle search form
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice 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 Use Mutate function in R
    How to Use Mutate function in R R
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • Crosstab calculation in R
    Crosstab calculation in R R
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • display the last value of each line in ggplot
    How to add labels at the end of each line in ggplot2? R

Leave a Reply Cancel reply

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




  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Is Data Science a Dying Profession?
  • How to Label Outliers in Boxplots in ggplot2?
  • Best Books About Data Analytics
  • How to Scale Only Numeric Columns in R
  • Best Books to Learn Statistics for Data Science

Check your inbox or spam folder to confirm your subscription.




 https://www.r-bloggers.com
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics
  • How to compare variances in R
    How to compare variances in R R
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R

Copyright © 2022 Data Science Tutorials.

Powered by PressBook News WordPress theme