Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort list files by semester and year

I am trying to sort a list files in a directory, I used differents libraries but all gave me the same result, example:

    myFiles <- paste0("Archivo_", c(1:2),"S",rep(c(2010:2015), each=2), ".txt") 

     # install.packages ('gtools')
     library ('gtools')
     mixedsort(myFiles)

    sort(myFiles)

    # install.packages("naturalsort")
     library("naturalsort")

    naturalsort(myFiles)

     [1] "Archivo_1S2010.txt" "Archivo_1S2011.txt" "Archivo_1S2012.txt"
     [4] "Archivo_1S2013.txt" "Archivo_1S2014.txt" "Archivo_1S2015.txt"
     [7] "Archivo_2S2010.txt" "Archivo_2S2011.txt" "Archivo_2S2012.txt"
    [10] "Archivo_2S2013.txt" "Archivo_2S2014.txt" "Archivo_2S2015.txt"

I would like get

 myFiles

  "Archivo_1S2010.txt" "Archivo_2S2010.txt" "Archivo_1S2011.txt"  
      "Archivo_2S2011.txt" "Archivo_1S2012.txt" "Archivo_2S2012.txt"
      "Archivo_1S2013.txt"  "Archivo_2S2013.txt"  "Archivo_1S2014.txt" 
      "Archivo_2S2014.txt"  "Archivo_1S2015.txt"  "Archivo_2S2015.txt"
 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  library(dplyr)
  myFiles %>% 
    tibble(archivo=.) %>% 
    mutate(archivo_ref = gsub("_\\dS", "", archivo)) %>% 
    arrange(archivo_ref) %>% 
    select(archivo) %>% 
    unlist %>% 
   unname
 [1] "Archivo_1S2010.txt" "Archivo_2S2010.txt" "Archivo_1S2011.txt" "Archivo_2S2011.txt"
 [5] "Archivo_1S2012.txt" "Archivo_2S2012.txt" "Archivo_1S2013.txt" "Archivo_2S2013.txt"
 [9] "Archivo_1S2014.txt" "Archivo_2S2014.txt" "Archivo_1S2015.txt" "Archivo_2S2015.txt"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading