How to sort alphabetically in R?

My files have the format:

ada1 ada2 ada3 .... ada10 ada11 ada12 

Unfortunately, when I write a10, a11 and a12 precede a2. Could you help me sort it alphabetically, as it should be?

#

Edit

Now I have thousands of these files. Basically, xyz1-12, abc1-12, etc.

I use the following to get all the files:

 GG <- grep("*.txt", list.files(), value = TRUE) 

Therefore, I can not manually "ada".

+2
source share
4 answers

Another way to use the gtools package:

 require(gtools) x <- paste0('a', 1:12) mixedsort(x) [1] "a1" "a2" "a3" "a4" "a5" "a6" "a7" "a8" "a9" "a10" "a11" "a12" 
+1
source

If there are always three characters, you can sort them by these characters, followed by numerical sorting of the rest of the string:

 GG <- paste0(c('ada', 'xyz'), 1:20) # Synthesis of data similar to what your command would give 

Using order with several arguments gives a permutation of the vector, and then indexing with this permutation returns the data in the desired sort order:

 GG[order(substring(GG, 1, 3), as.numeric(substring(GG, 4)))] [1] "ada1" "ada3" "ada5" "ada7" "ada9" "ada11" "ada13" "ada15" "ada17" "ada19" "xyz2" "xyz4" "xyz6" "xyz8" "xyz10" [16] "xyz12" "xyz14" "xyz16" "xyz18" "xyz20" 
+2
source

If you cannot change your names to something better (i.e. ada001, ada002 ...), you can create a double index. I assume that fnames is a vector with file names, and only a fixed number of letters precede the numbers.

 alpha <- substr(fnames, 1, 3) num <- as.integer(substr(fnames, 4, nchar(fnames))) o <- order(alpha, num) ## that your sorting vector 

You can modify this procedure to accommodate a different number of letters using regular expressions to find a split.

+1
source

If you can change the file names, you can do something like the following:

 names0 <- paste0("a", 1:20) temp <- strsplit(names0, "a") ind <- sapply(temp, "[[", 2) names1 <- paste0("a", sprintf("%03d", as.numeric(ind))) > names1 [1] "a001" "a002" "a003" "a004" "a005" "a006" [7] "a007" "a008" "a009" "a010" "a011" "a012" [13] "a013" "a014" "a015" "a016" "a017" "a018" [19] "a019" "a020" 

You may need to configure the sprintf call based on this.

To clarify using file.rename , it would be fairly simple to rename all your files.

0
source

All Articles