Sorting each row of a data frame
14,757
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))
Author by
user3276768
Updated on July 28, 2022Comments
-
user3276768 almost 2 years
I am trying to sort each row of a data frame using this line,
sapply(df, function(x) sort(x))
However, the columns are getting sorted instead of the rows.
For example, this data frame
5 10 7 1 5 6 3 9 2 4 4 5 1 3 3
is ending up like this:
4 3 1 1 3 5 5 7 2 4 6 10 9 3 5
And I want this:
1 5 5 7 10 2 3 4 6 9 1 3 3 4 5
Any recommendations? Thanks
-
Frank almost 9 yearsI had a similar idea:
do.call(rbind, lapply(split(df, seq_len(nrow(df))), sort))
I think any approach here involves converting each row to an atomic vector (so it can be sorted), which is roughly the same as converting the whole thing to matrix. -
Rorschach almost 9 years@Frank yea that works too, very slow for larger data though
-
Frank almost 9 yearsHm, maybe so -- that
t(sapply(
beatsdo.call(rbind,lapply(
-- though it's not obvious to me why. For the other part, transposing before splitting looks wasteful and inferior tosplit(df,seq_len(nrow(df)))
. If you have some benchmark in favor of transposing, I'd be surprised and interested. -
Rorschach almost 9 years@Frank yes, the first part is the slow part
-
Varun Gawande almost 3 yearsIf MARGIN was 2, the output would have been the Matrix, but sorted by the columns. Why do we have to transpose it while sorting according to Rows?