如何通过由长字符串组成的列对R数据进行排序?下面的例子说明我的问题:使用长字符串按列排序R数据帧
> a = matrix(NA, nrow=4, ncol=3)
> a[,1] = c(1,2,3,4)
> a[,2] = c("gene001_10M","gene002_10M","gene001_50M","gene002_50M")
> colnames(a) = c("value","sortkey","other")
> a = as.data.frame(a)
> a
value sortkey other
1 1 gene001_10M <NA>
2 2 gene002_10M <NA>
3 3 gene001_50M <NA>
4 4 gene002_50M <NA>
当我排序的“A”,现在,则SORTKEY似乎是从右至左读,留下“A”不变:
> b = a[sort(a$sortkey),]
> b
value sortkey other
1 1 gene001_10M <NA>
2 2 gene002_10M <NA>
3 3 gene001_50M <NA>
4 4 gene002_50M <NA>
我的目标,然而,就是:
> b
value sortkey other
1 1 gene001_10M <NA>
3 3 gene001_50M <NA>
2 2 gene002_10M <NA>
4 4 gene002_50M <NA>
好极了!谢谢。 – 2014-10-03 11:18:59