Open In App

How to sort each row of an R data frame in increasing order

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to sort each data frame row in increasing order using the R Programming Language.

How to sort each row in a data frame

R language offers various built-in functions to sort the rows in a data frame. By using the sorting functions provided by R, it is possible to arrange the rows in increasing or decreasing. Some of the methods to sort each row are.

Sorting a row in ascending order by using ‘apply()’ with ‘sort()’ function

This method is used to sort each row in increasing order.

Syntax:

apply(matrix or dataframe, MARGIN = 1, FUN = sort)

In this example, we created a data frame and sorted the each row in increasing order.

R
a=c(9,8,7,6,5,7,8,9,4,2)
b=c(8,7,1,5,9,4,2,1,6,7)
c=c(7,6,4,3,2,8,9,0,6,4)

dataframe<-data.frame(a,b,c)    
dataframe
print("sorting each row in increasing order")
t(apply(dataframe,1,sort))

Output:

   a b c
1 9 8 7
2 8 7 6
3 7 1 4
4 6 5 3
5 5 9 2
6 7 4 8
7 8 2 9
8 9 1 0
9 4 6 6
10 2 7 4

[1] "sorting each row in increasing order"
[,1] [,2] [,3]
[1,] 7 8 9
[2,] 6 7 8
[3,] 1 4 7
[4,] 3 5 6
[5,] 2 5 9
[6,] 4 7 8
[7,] 2 8 9
[8,] 0 1 9
[9,] 4 6 6
[10,] 2 4 7

In this example, we created a data frame and sorted the each row in increasing order.

R
#creating a data frame
df<-data.frame(vec1=c(9,8,7,6,5),
               vec2=c(3,5,6,7,8),
               vec3=c(7,6,8,5,4))     
print(df)
print("sorting each row in increasing order")
t(apply(df,1,sort))

Output:

  vec1 vec2 vec3
1 9 3 7
2 8 5 6
3 7 6 8
4 6 7 5
5 5 8 4

[1] "sorting each row in increasing order"
[,1] [,2] [,3]
[1,] 3 7 9
[2,] 5 6 8
[3,] 6 7 8
[4,] 5 6 7
[5,] 4 5 8

Sorting a row in ascending order by using ‘apply()’ with ‘order()’ function

These method is used to sort the each row in increasing order efficiently.

Syntax:

sorted_df <- apply(df_name, MARGIN=1 , function(x) x[order(x)])

In this example, we created a data frame and sorted the each row in increasing order.

R
# creating  dataframe
df1 <- data.frame(
  a = c(7,6,5,4,3,1,0),
  B = c(9,8,7,0,5,3,4),
  C = c(9,2,3,4,5,8,6)
)
 df1
# Sorting 
res <- t(apply(df1, 1, function(x) x[order(x)]))
print("sorting each row in increasing order")
print(res)

Output:

  a B C
1 7 9 9
2 6 8 2
3 5 7 3
4 4 0 4
5 3 5 5
6 1 3 8
7 0 4 6

[1] "sorting each row in increasing order"
[,1] [,2] [,3]
[1,] 7 9 9
[2,] 2 6 8
[3,] 3 5 7
[4,] 0 4 4
[5,] 3 5 5
[6,] 1 3 8
[7,] 0 4 6

In this example, we created a data frame and sorted the each row in increasing order.

R
vec1=c(4,3,5,1)
vec2=c(9,0,1,3)
vec3=c(8,5,4,9)
df1 <- data.frame(vec1,vec2,vec3)          
 df1
 
# Sorting 
res <- t(apply(df1, 1, function(x) x[order(x)]))
print("sorting each row in increasing order")
print(res)

Output:

vec1 vec2 vec3
1 4 9 8
2 3 0 5
3 5 1 4
4 1 3 9

[1] "sorting each row in increasing order"
[,1] [,2] [,3]
[1,] 4 8 9
[2,] 0 3 5
[3,] 1 4 5
[4,] 1 3 9

Conclusion

In conclusion, we learned about how to sort each row of a data frame in increasing order. By using the built-in functions provided by R, it is possible to sort the each row efficiently.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads