wiggle
wiggle

Reputation: 5

How do I order list by ending number in name of list element?

I have the following code that gives a list of several txt files, how do I re-order the list based on the last number (1770, 1780, 1790, 1800, 700, 710) such that the list order is now in increasing or decreasing order (700, 710, 1770, 1780, 1790, 1800)

> file_list <- list.files(path=folder, pattern="*.txt") 

> file_list

output:

 [78] "172010_001_122C_2.0_1770.txt"    
 [79] "172010_001_122C_2.0_1780.txt"    
 [80] "172010_001_122C_2.0_1790.txt"    
 [81] "172010_001_122C_2.0_1800.txt"    
 [82] "172010_001_122C_2.0_700.txt"     
 [83] "172010_001_122C_2.0_710.txt"     
 [84] "172010_001_122C_2.0_720.txt" 

Upvotes: 0

Views: 57

Answers (2)

Maurits Evers
Maurits Evers

Reputation: 50668

Here is a solution in base R using gsub

file_list <- c("172010_001_122C_2.0_1770.txt", 
               "172010_001_122C_2.0_1780.txt", 
               "172010_001_122C_2.0_700.txt")

file_list[order(as.numeric(gsub(".+_(\\d+)\\.txt$", "\\1", file_list)))]
# [1] "172010_001_122C_2.0_700.txt"  "172010_001_122C_2.0_1770.txt"
# [3] "172010_001_122C_2.0_1780.txt"

Explanation: We match an expression .+_ followed by digits \\d+ followed by .txt, and order by increasing digits.

Upvotes: 0

ekstroem
ekstroem

Reputation: 6151

There are several ways to do this. I prefer to work with regular expressions which can be handled with base R. Below there is an example using the stringr package

library(stringr)
## Create some example data
file_list <- c("172010_001_122C_2.0_1770.txt", 
               "172010_001_122C_2.0_1780.txt", 
               "172010_001_122C_2.0_700.txt")

The we extract the last part of the string before .txt. Note that the length of the value differs - otherwise we could have used the substr function directly and extracted the relevant characters. Here we extract all numbers right before .txt. Those are returned in a character matrix with the second column containing what we are after

result <- as.numeric(str_match(file_list, "(\\d+)\\.txt")[,2])
result
[1] 1770 1780  700

Then you can sort the file names

file_list[order(result)]
[1] "172010_001_122C_2.0_700.txt"  "172010_001_122C_2.0_1770.txt"
[3] "172010_001_122C_2.0_1780.txt"

Upvotes: 2

Related Questions