R – Sort Data Frame by Column
In this tutorial, we shall learn to sort a data frame by column in ascending order and descending order with example R scripts using R with function and R order function.
A data frame is a set of equal length objects.
Let us take aata frame as shown in the following.
df <- data.frame(names = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
The data frame has three columns : names, age, salary
We will sort these three columns in ascending or descending order in the following examples.
Sort in Ascending order
The syntax to sort a data frame in ascending order is
dataframe_name[with(dataframe_name, order(column_name)), ]
Example 1 – Sort Data Frame in Ascending Order
In this example, we will sort our data frame in ascending order.
r_dataframe_sort_asc.R
# R program to sort data frame by column in ascending order
df <- data.frame(names = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
cat("\n\n Sort data frame by names in ascending order\n")
# to sort data frame by names in ascending order
df_sorted_names_asc <- df[with(df, order(names)), ]
print(df_sorted_names_asc)
cat("\n\n Sort data frame by age in ascending order\n")
# to sort data frame by age in ascending order
df_sorted_age_asc <- df[with(df, order(age)), ]
print(df_sorted_age_asc)
cat("\n\n Sort data frame by income in ascending order\n")
# to sort data frame by income in ascending order
df_sorted_asc <- df[with(df, order(income)), ]
print(df_sorted_asc)
Output
$ Rscript r_dataframe_sort_asc.R
Sort data frame by names in ascending order
names age income
1 Andrew 28 25.2
6 Bing 23 11.5
3 Dany 49 11.0
5 John 38 44.0
2 Mathew 23 10.5
7 Monica 29 45.0
4 Philip 29 21.9
Sort data frame by age in ascending order
names age income
2 Mathew 23 10.5
6 Bing 23 11.5
1 Andrew 28 25.2
4 Philip 29 21.9
7 Monica 29 45.0
5 John 38 44.0
3 Dany 49 11.0
Sort data frame by income in ascending order
names age income
2 Mathew 23 10.5
3 Dany 49 11.0
6 Bing 23 11.5
4 Philip 29 21.9
1 Andrew 28 25.2
5 John 38 44.0
7 Monica 29 45.0
Sort in Descending order
The syntax to sort a data frame in descending order is
dataframe_name[with(dataframe_name, order(-column_name)), ]
Please observe the ‘-‘ negative sign before the column_name to sort in descending order
Example 2 – Sort Data Frame in Descending Order
In this example, we will sort our Data Frame in descending order.
r_dataframe_sort_desc.R
# R program to sort dataframe by column in descending order
df <- data.frame(names = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
cat("\n\n Sort data frame by names in descending order\n")
# to sort data frame by names in descending order
df_sorted_names_desc <- df[with(df, order(-names)), ]
print(df_sorted_names_desc)
cat("\n\n Sort data frame by age in descending order\n")
# to sort data frame by age in descending order
df_sorted_age_desc <- df[with(df, order(-age)), ]
print(df_sorted_age_desc)
cat("\n\n Sort data frame by income in descending order\n")
# to sort data frame by income in descending order
df_sorted_desc <- df[with(df, order(-income)), ]
print(df_sorted_desc)
Output
$ Rscript r_dataframe_sort_desc.R
Sort data frame by names in descending order
Warning message:
In Ops.factor(names) : ‘-’ not meaningful for factors
names age income
1 Andrew 28 25.2
2 Mathew 23 10.5
3 Dany 49 11.0
4 Philip 29 21.9
5 John 38 44.0
6 Bing 23 11.5
7 Monica 29 45.0
Sort data frame by age in descending order
names age income
3 Dany 49 11.0
5 John 38 44.0
4 Philip 29 21.9
7 Monica 29 45.0
1 Andrew 28 25.2
2 Mathew 23 10.5
6 Bing 23 11.5
Sort data frame by income in descending order
names age income
7 Monica 29 45.0
5 John 38 44.0
1 Andrew 28 25.2
4 Philip 29 21.9
6 Bing 23 11.5
3 Dany 49 11.0
2 Mathew 23 10.5
Note : Descending order is not applicable for Character Datatypes. Observe lines (5,6) with a warning message.
Conclusion
In this R Tutorial, we have learnt to sort a data frame by column in ascending and descending order.