To sort a dataframe in R, you can use order() or arrange() function from dplyr package.
The following methods show how you can do it with syntax.
Method 1: Use order() Function
# Sort dataframe in ascending order
df[order(df$column1), ]
# Sort dataframe in descending order
df[order(-df$column1), ]
Method 2: Use arrange() Function
library(dplyr)
# Sort in ascending order
df %>% arrange(column1)
# Sort in descending order
df %>% arrange(desc(column1))
The following examples show how to sort dataframe in R using two different approach.
Use order() Function
Let’s see how we can use order() function in R:
Sort Data Frame in Ascending Order
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Sort dataframe in ascending order
df= df[order(df$Value),]
# Print sorted dataframe
print(df)
Output:
Start_date Machine_name Value Reading
4 2003-04-27 Machine4 95 89
5 2004-11-26 Machine5 98 80
6 2008-11-25 Machine6 105 85
3 2001-01-19 Machine3 108 95
1 2000-05-21 Machine1 110 110
2 1998-03-28 Machine2 120 97
Here the output shows sorted dataframe in ascending order based on Value column in dataframe.
Sort Data Frame in Descending Order
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Sort dataframe in descending order
df= df[order(-df$Value),]
# Print sorted dataframe
print(df)
Output:
Start_date Machine_name Value Reading
2 1998-03-28 Machine2 120 97
1 2000-05-21 Machine1 110 110
3 2001-01-19 Machine3 108 95
6 2008-11-25 Machine6 105 85
5 2004-11-26 Machine5 98 80
4 2003-04-27 Machine4 95 89
Here the output shows dataframe which sorted in descending order based on Value column of dataframe.
Using arrange() Function
Let’s see how we can use arrange() function in R:
Sort Data Frame in Ascending Order Using dplyr Package
library(dplyr)
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Sort dataframe in ascending order
df %>% arrange(Value)
Output:
Start_date Machine_name Value Reading
1 2003-04-27 Machine4 95 89
2 2004-11-26 Machine5 98 80
3 2008-11-25 Machine6 105 85
4 2001-01-19 Machine3 108 95
5 2000-05-21 Machine1 110 110
6 1998-03-28 Machine2 120 97
Here the output shows dataframe sorted in ascending order based on Value column of dataframe using arrange() function.
Sort Data Frame in Descending Order Using dplyr Package
# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(110,120,108,95,98,105),Reading= c(110,97,95,89,80,85))
# Sort dataframe in descending order
df %>% arrange(desc(Value))
Output:
Start_date Machine_name Value Reading
1 1998-03-28 Machine2 120 97
2 2000-05-21 Machine1 110 110
3 2001-01-19 Machine3 108 95
4 2008-11-25 Machine6 105 85
5 2004-11-26 Machine5 98 80
6 2003-04-27 Machine4 95 89
Here the output shows dataframe which sorted in descending order using arrange() function from dplyr package.