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 vectors.

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

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -