How to sort a large number of csv files in ascending order in R?

R ProgrammingServer Side ProgrammingProgramming

To sort a large number of csv files in ascending order, we can use mixedsort function from gtools package. For example, if we have a list of csv files that are randomly arranged in a vector called FILES then the files can be sorted in ascending order using the command mixedsort(sort(FILES))

Example

 Live Demo

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

Output

[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"

Loading gtools package and sorting the files in vector Files1 in ascending order −

Example

library(gtools)
mixedsort(sort(Files1))

Output

[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"

Example

 Live Demo

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

Output

[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"

Example

mixedsort(sort(Files2))

Output

[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"
raja
Published on 10-Feb-2021 10:53:50
Advertisements