r - Detect alphabetical order in character vector -
i have several character vectors this:
lastname <- c("smith" ,"johnson" , "williams" , "moore" , "taylor", "jones" , "brown" , "davis" , "miller" , "wilson" )
lastname's last 4 elements in alphabetical order. want split vector alphabetical order starts. thus, result like:
lastname1 <- c("smith" ,"johnson" , "williams" , "moore" , "taylor", "jones") lastname2 <- c("brown" , "davis" , "miller" , "wilson" )
the part in alphabetical order located @ end it's length may differ.
any appreciated!
we can create logical index , split
vector index create list
of vector
s.
i1 <- rev(cumsum(c(true, diff(rank(rev(lastname))) >0))==1) split(lastname, i1) #$`false` #[1] "smith" "johnson" "williams" "moore" "taylor" "jones" #$`true` #[1] "brown" "davis" "miller" "wilson"
Comments
Post a Comment