按特定顺序排序data.frame(不上升/下降)

时间:2016-01-12 16:51:26

标签: r sorting dataframe

a = data.frame(x = c("fl", "kl", "po", "rt", "st"), a = runif(5), b = runif(5), c = runif(5), stringsAsFactors=FALSE)

reference = c("po", "rt", "fl", "st", "kl")

有data.frame a,它应根据x向量在reference上排序。我该怎么做?

2 个答案:

答案 0 :(得分:8)

使用match

match(reference, a$x)
#[1] 3 4 1 5 2

a[match(reference, a$x),]
#   x          a          b         c
#3 po 0.11479400 0.85043472 0.1972569
#4 rt 0.04886256 0.04916686 0.8341600
#1 fl 0.57528688 0.31122035 0.1204271
#5 st 0.77224495 0.29492122 0.9136916
#2 kl 0.67683487 0.87516455 0.9612954

解决data.frame中重复值问题的解决方案(尽管一个例子应该代表问题):

a = data.frame(x = c("fl", "kl", "po", "rt", "st", "st"), a = runif(6), 
               b = runif(6), c = runif(6), stringsAsFactors=FALSE)

a[order(factor(a$x, levels = reference)),]
#   x          a            b         c
#3 po 0.43392816 0.0008970879 0.7282965
#4 rt 0.74484047 0.4237432110 0.3593475
#1 fl 0.69747570 0.0441067580 0.1477714
#5 st 0.18866673 0.3650689835 0.4940101
#6 st 0.93842498 0.4953243888 0.4318899
#2 kl 0.09158319 0.6171634013 0.8798650

答案 1 :(得分:1)

试试这个:

a$reference <- sapply(a$x, function(x) which(x == reference))
a[order(a$reference), ]

您可以通过执行以下操作来避免保存到列中:

a[order(sapply(a$x, function(x) which(x == reference))), ]

如果你有更多的行重复x值,它可以工作,如下所示:

a2 <- rbind(a, a)
a2[order(sapply(a2$x, function(x) which(x == reference))), ]

输出如下所示:

    x          a         b          c
3  po 0.35188864 0.9611048 0.47784538
8  po 0.35188864 0.9611048 0.47784538
4  rt 0.40894400 0.7283944 0.56025326
9  rt 0.40894400 0.7283944 0.56025326
1  fl 0.04766363 0.9188573 0.05284394
6  fl 0.04766363 0.9188573 0.05284394
5  st 0.82095132 0.6863751 0.69826159
10 st 0.82095132 0.6863751 0.69826159
2  kl 0.70085309 0.2825283 0.39522013
7  kl 0.70085309 0.2825283 0.39522013