Chanti
Chanti

Reputation: 575

sort the strings based on last word in r

sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3     plyr_1.8.3      tidyr_0.3.1     gridExtra_2.0.0 scales_0.3.0   
[6] ggplot2_1.0.1   RPostgreSQL_0.4 DBI_0.3.1      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1      lubridate_1.3.3  assertthat_0.1   digest_0.6.8     MASS_7.3-44     
 [6] R6_2.1.1         grid_3.2.2       gtable_0.1.2     magrittr_1.5     stringi_0.5-5   
[11] reshape2_1.4.1   proto_0.3-10     tools_3.2.2      stringr_1.0.0    munsell_0.4.2   
[16] parallel_3.2.2   colorspace_1.2-6 memoise_0.2.1 

for example i have n strings in a column as shown below. I want to sort the strings based on the last word.

dput(dsp)
c("handlingstation / cropping/ forward / Linie 1", "handlingstation / cropping/ forward / Linie 2", 
"conveyorstation / Linie 1", "conveyorstation / Linie 2", "soft / handling / cleaning / backward / Linie 3", 
"jumper / doublejumper / Linie 1", "jumper / doublejumper / Linie 2"
)



dsp
[1] "handlingstation / cropping/ forward / Linie 1"  
[2] "handlingstation / cropping/ forward / Linie 2"  
[3] "conveyorstation / Linie 1"                      
[4] "conveyorstation / Linie 2"                      
[5] "soft / handling / cleaning / backward / Linie 3"
[6] "jumper / doublejumper / Linie 1"                
[7] "jumper / doublejumper / Linie 2" 

desired output

dsp_sorted
[1] "handlingstation / cropping/ forward / Linie 1"  
[2] "conveyorstation / Linie 1"                      
[3] "jumper / doublejumper / Linie 1"                
[4] "handlingstation / cropping/ forward / Linie 2"  
[5] "conveyorstation / Linie 2"                      
[6] "jumper / doublejumper / Linie 2"                
[7] "soft / handling / cleaning / backward / Linie 3"

I want all the strings in a prticular column to be ordered based on the last word. Here it should be based on Linie 1, Linie 2 and so on.

Could some one let me know how i can do these.

Upvotes: 2

Views: 434

Answers (1)

David Arenburg
David Arenburg

Reputation: 92282

You could try something as the following

dsp[order(sub(".*/ ", "", dsp))]
# [1] "handlingstation / cropping/ forward / Linie 1"   "conveyorstation / Linie 1"                      
# [3] "jumper / doublejumper / Linie 1"                 "handlingstation / cropping/ forward / Linie 2"  
# [5] "conveyorstation / Linie 2"                       "jumper / doublejumper / Linie 2"                
# [7] "soft / handling / cleaning / backward / Linie 3"

This is basically removes everything before the last appearance of / using regex and sorts your vector according to that word


Though in your case it will be probably be safer to resort to a mixed order operation (as you have numbers and characters in a single value)

library(gtools)
dsp[mixedorder(sub(".*/ ", "", dsp))]
# [1] "handlingstation / cropping/ forward / Linie 1"   "conveyorstation / Linie 1"                      
# [3] "jumper / doublejumper / Linie 1"                 "handlingstation / cropping/ forward / Linie 2"  
# [5] "conveyorstation / Linie 2"                       "jumper / doublejumper / Linie 2"                
# [7] "soft / handling / cleaning / backward / Linie 3"

Another option (depending on your real data) is to extract the numbers from the end of the string and sort accordingly

dsp[order(as.numeric(sub(".*(\\d+$)", "\\1", dsp)))]

Apparently the stringi package has a mixed order option too by specifying opts_collator = list(numeric = TRUE) while extracting the last word of a string, so you could also do

library(stringi)
dsp[stri_order(stri_extract_last_words(dsp), opts_collator = list(numeric = TRUE))]
# [1] "handlingstation / cropping/ forward / Linie 1"   "conveyorstation / Linie 1"                      
# [3] "jumper / doublejumper / Linie 1"                 "handlingstation / cropping/ forward / Linie 2"  
# [5] "conveyorstation / Linie 2"                       "jumper / doublejumper / Linie 2"                
# [7] "soft / handling / cleaning / backward / Linie 3"

Upvotes: 4

Related Questions