就计算机而言,它是正确分类的。但是,您可以mixedsort
从“gtools”包中获得所需的排序类型:
> 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"
以您的示例为例,这意味着您可以执行以下操作:
files <- list.files(pattern = ".txt")
library(gtools)
files <- mixedsort(files)
用户功能很有趣
由于编写小实用函数很容易,因此您也可以编写这样的小函数:
ListFiles <- function(pattern = ".txt") {
require(gtools)
myFiles <- list.files(pattern = pattern, )
mixedsort(myFiles)
}
然后,比较:
list.files(pattern = ".txt")
ListFiles(pattern = ".txt")