我需要弄清楚如何在R中按字母顺序对第一列中的字符串进行排序。
输入示例
AAAA A.A 0 0
ABAB A.B 0.046372 2.59202
ACAC A.C 0.108911 2.71921
ADAD A.D 0.054307 3.620057
AEAE A.E 0.042022 2.534175
AFAF A.F 0.043243 0.212713
AGAG A.G 0.046828 0.162782
AHAH A.H 0.02122 2.169073
AIAI A.I 0.038536 1.960308
AJAJ A.J 0.034669 1.954065
AOAO A.O 1.047243 3.799053
BABA B.A 0.046372 2.59202
BBBB B.B 0 0
BCBC B.C 0.100474 1.802687
BDBD B.D 0.051434 2.328003
BEBE B.E 0.041227 2.075464
BFBF B.F 0.039445 2.518254
BGBG B.G 0.019662 2.758563
BHBH B.H 0.050746 3.54119
BIBI B.I 0.033351 3.502687
BJBJ B.J 0.045264 3.407983
BOBO B.O 1.005559 -1
所需的输出:
AAAA A.A 0 0
AABB A.B 0.046372 2.59202
AABB B.A 0.046372 2.59202
等
提前谢谢!
答案 0 :(得分:1)
您可以使用sort
或vec <- c("AAAA","DEFA","AAAB","CBDA","AAAC","DEFG")
vec[order(vec)]
[1] "AAAA" "AAAB" "AAAC" "CBDA" "DEFA" "DEFG"
按字母顺序对字符向量进行排序(有关两者之间的差异,请参见@tim的注释)。例如:
vec <- sapply(vec, function(v) {paste0(sort(unlist(strsplit(v,""))),collapse = "")})
> vec
AAAA DEFA AAAB CBDA AAAC DEFG
"AAAA" "ADEF" "AAAB" "ABCD" "AAAC" "DEFG"
然后,如果要对字母序列中的字母重新排序,则需要将它们拆分并排序。可以通过执行以下操作来实现(感谢@ H1对功能的改进):
vec <- sort(sapply(vec, function(v) {paste0(sort(unlist(strsplit(v,""))),collapse = "")}))
> vec
[1] "AAAA" "AAAB" "AAAC" "ABCD" "ADEF" "DEFG"
如果要将两者结合起来,则需要这样做:
df
因此,在您的示例中,如果您的数据框名为df <- data.frame(vec = c("AAAA","DEFA","AAAB","CBDA","AAAC","DEFG"),
numb = c(1,2,3,4,5,6))
df[,1] <- as.character(df[,1])
df[,1] <- sapply(df[,1], function(v) { paste0(sort(unlist(strsplit(v,""))),collapse = "")})
df <- df[order(df[,1]),]
,则应执行以下操作:
> df
vec numb
1 AAAA 1
3 AAAB 3
5 AAAC 5
4 ABCD 4
2 ADEF 2
6 DEFG 6
您会得到:
var bg = document.getElementByClassName('bg');