In this article, we will discuss the arrange() function in R and how to use this function to arrange the values in a column in ascending and descending order.
What is the arrange() function in R?
The arrange() function in R is used to sort the values of columns in ascending or descending order.
Syntax:
arrange(.data, ...)
You can also use the Pipe operator (%>%):
.data %>% arrange(…)
Parameter:
- .data: a data frame.
- …: columns to arrange.
How to use this function?
We will use the arrange()
function to sort a variable in ascending and descending order.
We have a data frame containing the Height and Weight of several people.
Code:
# Create a data frame df <-data.frame( Height = c(156, 148, 174, 172, 165, 158, 169, 181, 162, 178), Weight = c(48, 39, 65, 59, 60, 45, 49, 70, 68, 61) ) print(df)
Output:
Height Weight
1 156 48
2 148 39
3 174 65
4 172 59
5 165 60
6 158 45
7 169 49
8 181 70
9 162 68
10 178 61
To use the arrange() function, you must import the ‘dplyr‘ library with the following command:
library(dplyr)
Ascending Order
We will use the arrange() function to sort the data frame by column ‘Height’ ascending.
Example:
# Import the 'dplyr' library library(dplyr) # Create a data frame df <-data.frame( Height = c(156, 148, 174, 172, 165, 158, 169, 181, 162, 178), Weight = c(48, 39, 65, 59, 60, 45, 49, 70, 68, 61) ) # Sort the data frame by column 'Height' in ascending dfsort <- arrange(df, Height) print(dfsort)
Output:
Height Weight
1 148 39
2 156 48
3 158 45
4 162 68
5 165 60
6 169 49
7 172 59
8 174 65
9 178 61
10 181 70
The original data frame has been sorted in ascending order of the Height column.
Descending Order
When used, the arrange() function will sort in ascending order by default. To sort in descending order, you must combine the arrange() function with the desc() function.
The desc()
function will transform a vector into a format sorted in descending order.
Syntax:
desc(x)
Parameter:
- x: vector to transform
We will use the arrange() function to sort the data frame by column ‘Weight’ descending.
Example:
# Import the 'dplyr' library library(dplyr) # Create a data frame df <-data.frame( Height = c(156, 148, 174, 172, 165, 158, 169, 181, 162, 178), Weight = c(48, 39, 65, 59, 60, 45, 49, 70, 68, 61) ) # Sort the data frame by column 'Weight' in descending dfsort <- arrange(df, desc(Weight)) print(dfsort)
Output:
Height Weight
1 181 70
2 162 68
3 174 65
4 178 61
5 165 60
6 172 59
7 169 49
8 156 48
9 158 45
10 148 39
The original data frame has been sorted in descending order of the Weight column.
Summary
The arrange() function in R is used to sort the values in the column. The default will be to sort in ascending order. If you want to sort the values in descending order, you can combine the desc() function with the arrange() function. Thank you for reading.
Maybe you are interested:
- What Is The summary() Function In R
- The gather() function in R
- The seq Function in R: How to use seq() in R
Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP