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.
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 ) ]
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)
If all of your data starts with "p " then you could just strip that off and coerce to numeric and use in order
.