是否可以使用R中的词法排序对字母数字值的向量进行排序? [英] Is it possible to sort a vector of alphanumeric values using lexical ordering in R?
问题描述
说,我有一个向量,如下所示:
Say, I have a vector as shown below:
v1<- c("p 1", "p 2", "p 10", "p 11")
使用sort(v1)
对其进行排序会给我
Sorting it using sort(v1)
gives me
[1] "p 1" "p 10" "p 11" "p 2"
但是我想要sort(v1)
I would however like to have sort(v1)
[1] "p 1" "p 2" "p 10" "p 11"
基于帮助文件,sort
似乎不允许词法排序.我不知道是否可以在不安装任何其他软件包的情况下完全按词法排序.
Based on the help file, sort
doesn't seem to allow lexical ordering. I wonder if lexical ordering is possible at all without installing any additional package.
推荐答案
您可以查看mixedsort
的代码,然后自己将其键入R.这样,您无需安装其他软件包就可以使用该功能.
You could look at the code for mixedsort
and type it into R yourself. Then you would have the function without installing an additional package.
或者您也可以在将字符串分割成段之后使用order
函数:
Or you can use the order
function after splitting the character strings into their pieces:
1 <- c('p 1', 'q 2','p 2','p 11', 'p 10')
sort(v1)
tmp <- strsplit(v1, ' +')
tmp1 <- sapply(tmp, '[[', 1)
tmp2 <- as.numeric(sapply(tmp, '[[', 2))
v1[ order( tmp1, tmp2 ) ]
或者您可以通过为xtfrm
编写方法并为vector提供适当的类来使其自动化:
Or you can automate this by writing a method for xtfrm
and giving your vector the appropriate class:
xtfrm.mixed <- function(x) {
tmp <- strsplit(x, ' +')
tmp1 <- sapply(tmp, '[[', 1)
tmp2 <- as.numeric(sapply(tmp, '[[', 2))
tmp3 <- rank(tmp1, ties.method='min')
tmp4 <- rank(tmp2, ties.method='min')
tmp3+tmp4/(max(tmp4)+1)
}
class(v1) <- 'mixed'
sort(v1)
如果所有数据均以"p"开头,则可以将其剥离并强制转换为数字,然后在order
中使用.
If all of your data starts with "p " then you could just strip that off and coerce to numeric and use in order
.
这篇关于是否可以使用R中的词法排序对字母数字值的向量进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!