How to use Arrange() method In R Language

For data transformation in R, we will use The arrange() method, to create an order for the sequence of the observations given. It takes a single column or a set of columns as the input to the method and creates an order for these. 

The arrange() method in the tidyverse package inputs a list of column names to rearrange them in a specified order. By default, the arrange() method arranges the data in ascending order. It has the following syntax : 

Syntax: arrange(col-name) 

Parameter:

col-name – Name of the column.

The data frame can be supplied with a pipe operator followed by the application of arrange() method to reflect the changes.

R




# Importing tidyvverse
library(tidyverse)
 
# Creating a data frame
data_frame = data.frame(
  col1 = c(2,4,1,7,5,3,5,8),
  col2 = letters[1:8],
  l3 = c(0,1,1,1,0,0,0,0))
 
# Assigning row names
rownames(data_frame) <- c("r1",
"r2","r3","r4","r5","r6","r7","r8")
print("Data Frame")
print(data_frame)
 
# Arranging a single column in ascending order
arr_data_frame <- data_frame %>% arrange(col1)
print("Arranged Data Frame")
print(arr_data_frame)


Output:

   col1 col2 col3
r1    2    a    0
r2    4    b    1
r3    1    c    1
r4    7    d    1
r5    5    e    0
r6    3    f    0
r7    5    g    0
r8    8    h    0

[1] "Arranged Data Frame"
   col1 col2 col3
r3    1    c    1
r1    2    a    0
r6    3    f    0
r2    4    b    1
r5    5    e    0
r7    5    g    0
r4    7    d    1
r8    8    h    0

Explanation : 

The minimum col1 value is 1, and then the largest is 8. All the col1 values in the data frame are arranged in ascending order, and the rows are shuffled accordingly. Similarly, we can arrange the data in descending order using desc() method inside arrange() method and the data frame rows are shuffled accordingly as in the below example.

The column values can also be arranged in descending order by specifying the order explicitly using the following syntax : 

Syntax: arrange(desc(col-name))

R




# Importing tidyverse
library(tidyverse)
 
# Creating a data frame
data_frame = data.frame(
  col1 = c(2,4,1,7,5,3,5,8),
  col2 = letters[1:8],
  col3 = c(0,1,1,1,0,0,0,0))
 
# Assigning row names
rownames(data_frame) <- c("r1",
"r2","r3","r4","r5","r6","r7","r8")
print("Data Frame")
# Printing data frame
print(data_frame)
 
# Arranging column in descending order
arr_data_frame <- data_frame %>%
              arrange(desc(col1))
print("Arranged Data Frame")
print(arr_data_frame)


Output:

 col1 col2 col3
r1    2    a    0
r2    4    b    1
r3    1    c    1
r4    7    d    1
r5    5    e    0
r6    3    f    0
r7    5    g    0
r8    8    h    0

[1] "Arranged Data Frame"
   col1 col2 col3
r8    8    h    0
r4    7    d    1
r5    5    e    0
r7    5    g    0
r2    4    b    1
r6    3    f    0
r1    2    a    0
r3    1    c    1

How to Transform Data in R?

In this article, we will learn how to transform data in the R programming language.

Similar Reads

Data Transformation in R

The data transformation in R is mostly handled by the external packages tidyverse and dplyr . These packages provide many methods to carry out the data simulations. There are a large number of ways to simulate data transformation in R. These methods are widely available using these packages, which can be downloaded and installed using the following command :...

Method 1: Using Arrange() method

For data transformation in R, we will use The arrange() method, to create an order for the sequence of the observations given. It takes a single column or a set of columns as the input to the method and creates an order for these....

Method 2: Using select() method

...

Method 3: Using filter() method

...

Method 4: Using spread() method

Data transformation in R of the data frame can also be fetched using the select() method in tidyverse package. The columns are fetched in the order of their specification in the argument list of the select() method call. This method results in a subset of the data frame as the output. The following syntax is followed :...

Method 5: Using mutate() method

...

Method 6: Using group_by() and summarise() method

...

Method 7: Using the gather() method

The filter() method in the tidyverse package is used to apply a range of constraints and conditions to the column values of the data frame in data transformation in R. It filters the data and results in the smaller output returned by the column values satisfying the specified condition. The conditions are specified using the logical operators, and values are validated then.  A data frame can be supplied with the pipe operator and then using the filter condition....