如何基于定界符之前的值对R中的矢量元素进行排序

时间:2019-02-12 17:26:30

标签: r sorting

我有一个称为myvec的向量。我想根据-之前的数字对值进行排序。有什么建议吗?

myvec <- c("2-1_16S_S217_R1_001.fastq", "2-2_16S_S226_R1_001.fastq", "3-1_16S_S234_R1_001.fastq", 
"3-2_16S_S242_R1_001.fastq", "11-1_16S_S199_R1_001.fastq", "1-1_16S_S197_R1_001.fastq", 
"11-2_16S_S209_R1_001.fastq", "1-2_16S_S207_R1_001.fastq")

此外,当我做sort (myvec)时,我得到:

[1] "1-1_16S_S197_R1_001.fastq"  "1-2_16S_S207_R1_001.fastq"  "11-1_16S_S199_R1_001.fastq" "11-2_16S_S209_R1_001.fastq"
[5] "2-1_16S_S217_R1_001.fastq"  "2-2_16S_S226_R1_001.fastq"  "3-1_16S_S234_R1_001.fastq"  "3-2_16S_S242_R1_001.fastq"

也尝试过

require('gtools')
mixedsort(myvec)

给出:

[1] "1-2_16S_S207_R1_001.fastq"  "1-1_16S_S197_R1_001.fastq"  "2-2_16S_S226_R1_001.fastq"  "2-1_16S_S217_R1_001.fastq" 
[5] "3-2_16S_S242_R1_001.fastq"  "3-1_16S_S234_R1_001.fastq"  "11-2_16S_S209_R1_001.fastq" "11-1_16S_S199_R1_001.fastq"

我想要的结果:

1-1_16S_S197_R1_001.fastq
1-2_16S_S207_R1_001.fastq
2-1_16S_S217_R1_001.fastq
2-2_16S_S226_R1_001.fastq
3-1_16S_S234_R1_001.fastq
3-2_16S_S242_R1_001.fastq
11-1_16S_S199_R1_001.fastq
11-1_16S_S199_R1_001.fastq

3 个答案:

答案 0 :(得分:2)

我们也可以使用str_sort中的stringr

stringr:::str_sort(myvec, numeric = TRUE)
# [1] "1-1_16S_S197_R1_001.fastq" 
# [2] "1-2_16S_S207_R1_001.fastq" 
# [3] "2-1_16S_S217_R1_001.fastq" 
# [4] "2-2_16S_S226_R1_001.fastq" 
# [5] "3-1_16S_S234_R1_001.fastq" 
# [6] "3-2_16S_S242_R1_001.fastq" 
# [7] "11-1_16S_S199_R1_001.fastq"
# [8] "11-2_16S_S209_R1_001.fastq"

答案 1 :(得分:1)

> myvec[order(as.numeric(sub("(^\\d+).*", "\\1", myvec)))]

[1] "1-1_16S_S197_R1_001.fastq" 
[2] "1-2_16S_S207_R1_001.fastq" 
[3] "2-1_16S_S217_R1_001.fastq" 
[4] "2-2_16S_S226_R1_001.fastq" 
[5] "3-1_16S_S234_R1_001.fastq" 
[6] "3-2_16S_S242_R1_001.fastq" 
[7] "11-1_16S_S199_R1_001.fastq"
[8] "11-2_16S_S209_R1_001.fastq"

答案 2 :(得分:1)

一种选择是获取上面带有parse_numberorder的数字

myvec[order(readr::parse_number(myvec))]
#[1] "1-1_16S_S197_R1_001.fastq"  "1-2_16S_S207_R1_001.fastq"  
#[3] "2-1_16S_S217_R1_001.fastq"  "2-2_16S_S226_R1_001.fastq" 
#[5] "3-1_16S_S234_R1_001.fastq"  "3-2_16S_S242_R1_001.fastq"
#[7] "11-1_16S_S199_R1_001.fastq" "11-2_16S_S209_R1_001.fastq"

或者我们要根据第一个-之前的字母数字字符进行排序,然后使用str_extract(来自stringr)提取子字符串,使用mixedorder(来自{{ 1}})对向量进行排序

gtools