首页 > r > R排序列

R排序列 (R sorting column)

2012-06-29 r

问题

我有一个文件,在R中加载了几个列。我想要实现的是将文件列的顺序排列在每列中的最小值。例如

输入(minA = 0,minB = 3,minC = 1)
ABC
4 8 1
2 3 4
0 3 1

输出
ABC
4 1 8
2 4 3
0 1 3

解决方法

您可以尝试apply()这种方式:

mins <- apply(myData, 2, min)
o <- order(mins)
myData <- myData[,o]

问题

I have a file with several column loaded in R. What I want to achieve is to order the column of the file respect to the minimal value in each of the column. E.g.

Input(minA=0,minB=3,minC=1)
A B C
4 8 1
2 3 4
0 3 1

Output
A B C
4 1 8
2 4 3
0 1 3

解决方法

You might try apply() this way:

mins <- apply(myData, 2, min)
o <- order(mins)
myData <- myData[,o]
相似信息