Reputation: 3207
Just trying to do a natural sort of a vector using gtools::mixedsort
, but I want a certain value to always show up last... is there a clean way to do it? Or should I remove those values from the vector before sorting and then add them manually? Thanks!
In the MWE below I want "na" last:
> aa <- c("235dfh","na","addk20","vcxvz89dg","REni0","na","235dfh")
> gtools::mixedsort(unique(aa))
[1] "235dfh" "addk20" "na" "REni0" "vcxvz89dg"
Upvotes: 1
Views: 243
Reputation: 389047
This is kind of hack :
aa[aa == 'na'] <- NA
bb <- sort(unique(aa), na.last = TRUE)
bb
#[1] "235dfh" "addk20" "REni0" "vcxvz89dg" NA
and then put the value back if needed.
bb[is.na(bb)] <- 'na'
#[1] "235dfh" "addk20" "REni0" "vcxvz89dg" "na"
Or using setdiff
:
c(sort(setdiff(aa, 'na')), 'na')
Upvotes: 2