I have a simple question about the list.files()
function. I have a folder containing a list of files named in this way:
DF2.txt
DF3.txt
DF4.txt
DF5.txt
.......
.......
When I paste the following string,
files <- list.files(pattern = ".txt")
The vector returns values in this order:
"DF10.txt"
"DF11.txt"
"DF12.txt"
........
........
"DF2.txt"
"DF20.txt"
"DF21.txt"
.........
.........
"DF3.txt"
"DF30.txt"
"DF31.txt"
..........
..........
and so on. I would like to list the files in numerical increasing order as they appear in the folder. Why does R change the order of the files in the folder after list.files()
and how can I rearrange these to match the original order?
To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.
List files and sort by file size. Type the ls -S (the S is uppercase) command to list files or directories and sort by size in descending order (biggest to smallest).
Listing files in reverse name order To reverse the listing of files by name, add the -r (reverse) option. This will be like turning the normal listing upside down.
Steps to find Largest directories in Linux du command : Estimate file space usage. sort command : Sort lines of text files or given input data. head command : Output the first part of files i.e. to display first 10 largest file. find command : Search file.
As far as computers are concerned, it is sorting correctly. However, you can used mixedsort
from the "gtools" package to get the type of sorting you want:
> myFiles <- paste("file", 1:20, ".txt", sep = "")
> sort(myFiles)
[1] "file10.txt" "file11.txt" "file12.txt" "file13.txt" "file14.txt" "file15.txt"
[7] "file16.txt" "file17.txt" "file18.txt" "file19.txt" "file1.txt" "file20.txt"
[13] "file2.txt" "file3.txt" "file4.txt" "file5.txt" "file6.txt" "file7.txt"
[19] "file8.txt" "file9.txt"
> library(gtools)
> mixedsort(sort(myFiles))
[1] "file1.txt" "file2.txt" "file3.txt" "file4.txt" "file5.txt" "file6.txt"
[7] "file7.txt" "file8.txt" "file9.txt" "file10.txt" "file11.txt" "file12.txt"
[13] "file13.txt" "file14.txt" "file15.txt" "file16.txt" "file17.txt" "file18.txt"
[19] "file19.txt" "file20.txt"
With your example, that means you can do:
files <- list.files(pattern = ".txt")
library(gtools)
files <- mixedsort(files)
Since it's easy to write little utility functions, you can also write a little function like this:
ListFiles <- function(pattern = ".txt") {
require(gtools)
myFiles <- list.files(pattern = pattern, )
mixedsort(myFiles)
}
Then, compare:
list.files(pattern = ".txt")
ListFiles(pattern = ".txt")
This is a naturalsort vs alphabetical sort problem. For me the package called naturalsort works best for displaying filenames in a human readable way.
> #install.packages("naturalsort")
> library("naturalsort")
> x <- paste(c("2file1","2file2","10file1.2","10file0.2","20file1","100",""))
> naturalsort(x)
[1] "" "2file1" "2file2" "10file0.2" "10file1.2" "20file1" "100"
The numbers are sorted alphabetically. For a base R approach you could do something like:
dat = sort(paste("DF", 1:100, ".txt", sep = ""))
numbers = as.numeric(regmatches(dat, regexpr("[0-9]+", dat)))
dat[order(numbers)]
[1] "DF1.txt" "DF2.txt" "DF3.txt" "DF4.txt" "DF5.txt" "DF6.txt"
[7] "DF7.txt" "DF8.txt" "DF9.txt" "DF10.txt" "DF11.txt" "DF12.txt"
[13] "DF13.txt" "DF14.txt" "DF15.txt" "DF16.txt" "DF17.txt" "DF18.txt"
[19] "DF19.txt" "DF20.txt" "DF21.txt" "DF22.txt" "DF23.txt" "DF24.txt"
[25] "DF25.txt" "DF26.txt" "DF27.txt" "DF28.txt" "DF29.txt" "DF30.txt"
[31] "DF31.txt" "DF32.txt" "DF33.txt" "DF34.txt" "DF35.txt" "DF36.txt"
[37] "DF37.txt" "DF38.txt" "DF39.txt" "DF40.txt" "DF41.txt" "DF42.txt"
[43] "DF43.txt" "DF44.txt" "DF45.txt" "DF46.txt" "DF47.txt" "DF48.txt"
[49] "DF49.txt" "DF50.txt" "DF51.txt" "DF52.txt" "DF53.txt" "DF54.txt"
[55] "DF55.txt" "DF56.txt" "DF57.txt" "DF58.txt" "DF59.txt" "DF60.txt"
[61] "DF61.txt" "DF62.txt" "DF63.txt" "DF64.txt" "DF65.txt" "DF66.txt"
[67] "DF67.txt" "DF68.txt" "DF69.txt" "DF70.txt" "DF71.txt" "DF72.txt"
[73] "DF73.txt" "DF74.txt" "DF75.txt" "DF76.txt" "DF77.txt" "DF78.txt"
[79] "DF79.txt" "DF80.txt" "DF81.txt" "DF82.txt" "DF83.txt" "DF84.txt"
[85] "DF85.txt" "DF86.txt" "DF87.txt" "DF88.txt" "DF89.txt" "DF90.txt"
[91] "DF91.txt" "DF92.txt" "DF93.txt" "DF94.txt" "DF95.txt" "DF96.txt"
[97] "DF97.txt" "DF98.txt" "DF99.txt" "DF100.txt"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With