用R中的字符和数字对向量排序

用R中的字符和数字对向量排序,r,R,我想把向量分类为 m=c(1),我们将'm'和'n'的名称设置为'0'和'1',将其连接到单个向量,并通过提取新向量中的数字部分(使用gsub)将其转换为数字,顺序并使用该索引对'n1'进行排序 n1 <- c(setNames(m, rep(0, length(m))),setNames(n, rep(1, length(n)))) r1 <- n1[order(as.numeric(gsub("\\D+", "", n1)))] as.vector(r1) #[1] "1"

我想把向量分类为


m=c(1),我们将'm'和'n'的
名称设置为'0'和'1',将其连接到单个
向量
,并通过提取新向量中的数字部分(使用
gsub
)将其转换为
数字
顺序
并使用该索引对'n1'进行排序

n1 <- c(setNames(m, rep(0, length(m))),setNames(n, rep(1, length(n))))
r1 <- n1[order(as.numeric(gsub("\\D+", "", n1)))]

as.vector(r1)
#[1] "1"   "2"   "<4"  "<4"  "5"   "7"   "8"   "<12" "15"  "17"  "18"  "20"  
#[13]  "<21" "<25" "25"  "27"  "34"  "<35" "40"  "43" 

as.integer(names(r1))
#[1] 0 1 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 1 1

n1我们用'0'和'1'设置'm'和'n'的
名称
,将其连接到单个
向量
,并通过提取新向量中的数字部分(使用
gsub
)将其转换为
数字
顺序
并使用该索引对'n1'进行排序

n1 <- c(setNames(m, rep(0, length(m))),setNames(n, rep(1, length(n))))
r1 <- n1[order(as.numeric(gsub("\\D+", "", n1)))]

as.vector(r1)
#[1] "1"   "2"   "<4"  "<4"  "5"   "7"   "8"   "<12" "15"  "17"  "18"  "20"  
#[13]  "<21" "<25" "25"  "27"  "34"  "<35" "40"  "43" 

as.integer(names(r1))
#[1] 0 1 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 1 1 1
n1