Sorting each row of a data frame [duplicate]
Solution 1:
You could use the plain apply
function with MARGIN = 1
to apply over rows and then transpose the result.
t(apply(df, 1, sort))
Solution 2:
You can transpose it (coverts it to matrix), and split by column and sort
t(sapply(split(t(df), col(t(df))), sort))
# [,1] [,2] [,3] [,4] [,5]
# 1 1 5 5 7 10
# 2 2 3 4 6 9
# 3 1 3 3 4 5
Because a data.frame is a list of columns, when you sapply
like that you are sorting the columns.
or apply
by row
t(apply(df, 1, sort))