如何在 R 中以升序对大量 csv 文件进行排序?


若要以升序对大量 csv 文件进行排序,我们可以使用 gtools 软件包中的 mixedsort 函数。例如,如果我们有一个随机排列在称为 FILES 的向量中的 csv 文件列表,那么可以使用命令 mixedsort(sort(FILES)) 以升序对文件进行排序。

示例

 在线演示

Files1<-sample(c(paste("file",1:50,".csv",sep="")),150,replace=TRUE)
Files1

输出

[1] "file10.csv" "file27.csv" "file3.csv" "file37.csv" "file18.csv"
[6] "file27.csv" "file45.csv" "file23.csv" "file44.csv" "file37.csv"
[11] "file7.csv" "file48.csv" "file13.csv" "file13.csv" "file2.csv"
[16] "file21.csv" "file22.csv" "file34.csv" "file18.csv" "file5.csv"
[21] "file36.csv" "file15.csv" "file20.csv" "file50.csv" "file11.csv"
[26] "file45.csv" "file1.csv" "file39.csv" "file47.csv" "file33.csv"
[31] "file3.csv" "file37.csv" "file38.csv" "file35.csv" "file6.csv"
[36] "file11.csv" "file13.csv" "file18.csv" "file29.csv" "file40.csv"
[41] "file40.csv" "file1.csv" "file16.csv" "file50.csv" "file36.csv"
[46] "file20.csv" "file37.csv" "file2.csv" "file27.csv" "file46.csv"
[51] "file29.csv" "file2.csv" "file2.csv" "file41.csv" "file42.csv"
[56] "file6.csv" "file4.csv" "file48.csv" "file16.csv" "file30.csv"
[61] "file29.csv" "file18.csv" "file47.csv" "file26.csv" "file36.csv"
[66] "file8.csv" "file48.csv" "file5.csv" "file5.csv" "file24.csv"
[71] "file34.csv" "file26.csv" "file33.csv" "file3.csv" "file26.csv"
[76] "file8.csv" "file6.csv" "file30.csv" "file48.csv" "file5.csv"
[81] "file14.csv" "file44.csv" "file29.csv" "file48.csv" "file12.csv"
[86] "file30.csv" "file24.csv" "file5.csv" "file36.csv" "file20.csv"
[91] "file14.csv" "file44.csv" "file38.csv" "file41.csv" "file20.csv"
[96] "file11.csv" "file34.csv" "file24.csv" "file34.csv" "file15.csv"
[101] "file16.csv" "file41.csv" "file39.csv" "file17.csv" "file46.csv"
[106] "file32.csv" "file33.csv" "file48.csv" "file26.csv" "file1.csv"
[111] "file16.csv" "file17.csv" "file8.csv" "file23.csv" "file44.csv"
[116] "file21.csv" "file3.csv" "file18.csv" "file28.csv" "file40.csv"
[121] "file40.csv" "file20.csv" "file30.csv" "file38.csv" "file6.csv"
[126] "file30.csv" "file44.csv" "file32.csv" "file4.csv" "file40.csv"
[131] "file25.csv" "file42.csv" "file6.csv" "file26.csv" "file11.csv"
[136] "file34.csv" "file7.csv" "file11.csv" "file35.csv" "file44.csv"
[141] "file1.csv" "file15.csv" "file17.csv" "file33.csv" "file45.csv"
[146] "file34.csv" "file30.csv" "file39.csv" "file16.csv" "file2.csv"

加载 gtools 软件包,并以升序对向量 Files1 中的文件进行排序-

示例

library(gtools)
mixedsort(sort(Files1))

输出

[1] "file1.csv" "file1.csv" "file1.csv" "file1.csv" "file2.csv"
[6] "file2.csv" "file2.csv" "file2.csv" "file2.csv" "file3.csv"
[11] "file3.csv" "file3.csv" "file3.csv" "file4.csv" "file4.csv"
[16] "file5.csv" "file5.csv" "file5.csv" "file5.csv" "file5.csv"
[21] "file6.csv" "file6.csv" "file6.csv" "file6.csv" "file6.csv"
[26] "file7.csv" "file7.csv" "file8.csv" "file8.csv" "file8.csv"
[31] "file10.csv" "file11.csv" "file11.csv" "file11.csv" "file11.csv"
[36] "file11.csv" "file12.csv" "file13.csv" "file13.csv" "file13.csv"
[41] "file14.csv" "file14.csv" "file15.csv" "file15.csv" "file15.csv"
[46] "file16.csv" "file16.csv" "file16.csv" "file16.csv" "file16.csv"
[51] "file17.csv" "file17.csv" "file17.csv" "file18.csv" "file18.csv"
[56] "file18.csv" "file18.csv" "file18.csv" "file20.csv" "file20.csv"
[61] "file20.csv" "file20.csv" "file20.csv" "file21.csv" "file21.csv"
[66] "file22.csv" "file23.csv" "file23.csv" "file24.csv" "file24.csv"
[71] "file24.csv" "file25.csv" "file26.csv" "file26.csv" "file26.csv"
[76] "file26.csv" "file26.csv" "file27.csv" "file27.csv" "file27.csv"
[81] "file28.csv" "file29.csv" "file29.csv" "file29.csv" "file29.csv"
[86] "file30.csv" "file30.csv" "file30.csv" "file30.csv" "file30.csv"
[91] "file30.csv" "file32.csv" "file32.csv" "file33.csv" "file33.csv"
[96] "file33.csv" "file33.csv" "file34.csv" "file34.csv" "file34.csv"
[101] "file34.csv" "file34.csv" "file34.csv" "file35.csv" "file35.csv"
[106] "file36.csv" "file36.csv" "file36.csv" "file36.csv" "file37.csv"
[111] "file37.csv" "file37.csv" "file37.csv" "file38.csv" "file38.csv"
[116] "file38.csv" "file39.csv" "file39.csv" "file39.csv" "file40.csv"
[121] "file40.csv" "file40.csv" "file40.csv" "file40.csv" "file41.csv"
[126] "file41.csv" "file41.csv" "file42.csv" "file42.csv" "file44.csv"
[131] "file44.csv" "file44.csv" "file44.csv" "file44.csv" "file44.csv"
[136] "file45.csv" "file45.csv" "file45.csv" "file46.csv" "file46.csv"
[141] "file47.csv" "file47.csv" "file48.csv" "file48.csv" "file48.csv"
[146] "file48.csv" "file48.csv" "file48.csv" "file50.csv" "file50.csv"

示例

 在线演示

Files2<-sample(c(paste(1:50,"file",".csv",sep="")),150,replace=TRUE)
Files2

输出

[1] "43file.csv" "15file.csv" "6file.csv" "28file.csv" "46file.csv"
[6] "31file.csv" "6file.csv" "16file.csv" "36file.csv" "34file.csv"
[11] "24file.csv" "49file.csv" "38file.csv" "9file.csv" "30file.csv"
[16] "16file.csv" "46file.csv" "19file.csv" "11file.csv" "41file.csv"
[21] "26file.csv" "23file.csv" "36file.csv" "34file.csv" "28file.csv"
[26] "48file.csv" "40file.csv" "38file.csv" "43file.csv" "2file.csv"
[31] "18file.csv" "18file.csv" "15file.csv" "6file.csv" "11file.csv"
[36] "12file.csv" "36file.csv" "10file.csv" "31file.csv" "5file.csv"
[41] "2file.csv" "24file.csv" "47file.csv" "14file.csv" "23file.csv"
[46] "5file.csv" "34file.csv" "20file.csv" "44file.csv" "30file.csv"
[51] "2file.csv" "46file.csv" "41file.csv" "31file.csv" "46file.csv"
[56] "27file.csv" "14file.csv" "46file.csv" "48file.csv" "22file.csv"
[61] "37file.csv" "34file.csv" "45file.csv" "41file.csv" "4file.csv"
[66] "12file.csv" "43file.csv" "47file.csv" "46file.csv" "42file.csv"
[71] "30file.csv" "45file.csv" "19file.csv" "10file.csv" "23file.csv"
[76] "24file.csv" "5file.csv" "3file.csv" "31file.csv" "15file.csv"
[81] "46file.csv" "3file.csv" "41file.csv" "43file.csv" "40file.csv"
[86] "11file.csv" "30file.csv" "39file.csv" "50file.csv" "11file.csv"
[91] "10file.csv" "31file.csv" "41file.csv" "22file.csv" "12file.csv"
[96] "32file.csv" "28file.csv" "17file.csv" "10file.csv" "6file.csv"
[101] "31file.csv" "9file.csv" "35file.csv" "36file.csv" "46file.csv"
[106] "12file.csv" "19file.csv" "37file.csv" "49file.csv" "5file.csv"
[111] "32file.csv" "27file.csv" "4file.csv" "46file.csv" "12file.csv"
[116] "15file.csv" "34file.csv" "15file.csv" "37file.csv" "3file.csv"
[121] "7file.csv" "33file.csv" "39file.csv" "14file.csv" "11file.csv"
[126] "25file.csv" "27file.csv" "19file.csv" "40file.csv" "48file.csv"
[131] "15file.csv" "7file.csv" "26file.csv" "25file.csv" "17file.csv"
[136] "23file.csv" "12file.csv" "49file.csv" "40file.csv" "50file.csv"
[141] "32file.csv" "16file.csv" "50file.csv" "48file.csv" "48file.csv"
[146] "35file.csv" "31file.csv" "33file.csv" "50file.csv" "27file.csv"

示例

mixedsort(sort(Files2))

输出

[1] "2file.csv" "2file.csv" "2file.csv" "3file.csv" "3file.csv"
[6] "3file.csv" "4file.csv" "4file.csv" "5file.csv" "5file.csv"
[11] "5file.csv" "5file.csv" "6file.csv" "6file.csv" "6file.csv"
[16] "6file.csv" "7file.csv" "7file.csv" "9file.csv" "9file.csv"
[21] "10file.csv" "10file.csv" "10file.csv" "10file.csv" "11file.csv"
[26] "11file.csv" "11file.csv" "11file.csv" "11file.csv" "12file.csv"
[31] "12file.csv" "12file.csv" "12file.csv" "12file.csv" "12file.csv"
[36] "14file.csv" "14file.csv" "14file.csv" "15file.csv" "15file.csv"
[41] "15file.csv" "15file.csv" "15file.csv" "15file.csv" "16file.csv"
[46] "16file.csv" "16file.csv" "17file.csv" "17file.csv" "18file.csv"
[51] "18file.csv" "19file.csv" "19file.csv" "19file.csv" "19file.csv"
[56] "20file.csv" "22file.csv" "22file.csv" "23file.csv" "23file.csv"
[61] "23file.csv" "23file.csv" "24file.csv" "24file.csv" "24file.csv"
[66] "25file.csv" "25file.csv" "26file.csv" "26file.csv" "27file.csv"
[71] "27file.csv" "27file.csv" "27file.csv" "28file.csv" "28file.csv"
[76] "28file.csv" "30file.csv" "30file.csv" "30file.csv" "30file.csv"
[81] "31file.csv" "31file.csv" "31file.csv" "31file.csv" "31file.csv"
[86] "31file.csv" "31file.csv" "32file.csv" "32file.csv" "32file.csv"
[91] "33file.csv" "33file.csv" "34file.csv" "34file.csv" "34file.csv"
[96] "34file.csv" "34file.csv" "35file.csv" "35file.csv" "36file.csv"
[101] "36file.csv" "36file.csv" "36file.csv" "37file.csv" "37file.csv"
[106] "37file.csv" "38file.csv" "38file.csv" "39file.csv" "39file.csv"
[111] "40file.csv" "40file.csv" "40file.csv" "40file.csv" "41file.csv"
[116] "41file.csv" "41file.csv" "41file.csv" "41file.csv" "42file.csv"
[121] "43file.csv" "43file.csv" "43file.csv" "43file.csv" "44file.csv"
[126] "45file.csv" "45file.csv" "46file.csv" "46file.csv" "46file.csv"
[131] "46file.csv" "46file.csv" "46file.csv" "46file.csv" "46file.csv"
[136] "46file.csv" "47file.csv" "47file.csv" "48file.csv" "48file.csv"
[141] "48file.csv" "48file.csv" "48file.csv" "49file.csv" "49file.csv"
[146] "49file.csv" "50file.csv" "50file.csv" "50file.csv" "50file.csv"

更新于: 2021年2月10日

211 个浏览量

开启您的 职业生涯

完成课程获得认证

立即开始
广告