对于R中的每一行排序第二至第五列 [英] Sort second to fifth column for each row in R
本文介绍了对于R中的每一行排序第二至第五列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 R 中有一个数据框,我需要独立排列第二至第五列的每一行。
I have a data frame in R, and I need to sort each row for the second to fifth column independently.
例如,我有以下
1 2 3 1 4 5
2 1 3 2 6 7
3 7 2 1 3 2
...
我需要得到
1 1 2 3 4 5
2 1 2 3 6 7
3 1 2 2 3 7
...
如何快速而美观?
推荐答案
这样的东西可能很有用
> cbind(df[,1], t(apply(df[,-1], 1, sort)))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 2 3 4 5
[2,] 2 1 2 3 6 7
[3,] 3 1 2 2 3 7
其中 df
是一个data.frame:
where df
is a data.frame:
df <- read.table(text="1 2 3 1 4 5
2 1 3 2 6 7
3 7 2 1 3 2", header=FALSE)
这篇关于对于R中的每一行排序第二至第五列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文