Hi R, My csv files are stored in the order, '1abc.csv', '2def.csv', '3ghi.csv', '10files.csv' in a folder. When I read this into R from list.files (R command: x=list.files("Z:/CSV/fold",full.names=F), I don't get the same order, instead I get the order as "10files.csv" "1abc.csv" "2def.csv" "3ghi.csv". But I don't want this ordering. So, how do I maintain the oder which I have in my physical folder? Thanks in advance Shubha [[alternative HTML version deleted]]
Rename them so that they all have the same number of zero filled leading digits: 01abc.csv, 02def.csv, ... '10' comes before '1a' in the stardard sorting sequence. The other way is to get a list of all your files, parse off the numerics at the beginning and then sort in numerical order. On 5/18/07, Shubha Vishwanath Karanth <shubhak@ambaresearch.com> wrote:> > Hi R, > > > > My csv files are stored in the order, '1abc.csv', '2def.csv', > '3ghi.csv', '10files.csv' in a folder. When I read this into R from > list.files (R command: x=list.files("Z:/CSV/fold",full.names=F), I don't > get the same order, instead I get the order as "10files.csv" "1abc.csv" > "2def.csv" "3ghi.csv". But I don't want this ordering. So, how do I > maintain the oder which I have in my physical folder? > > > > Thanks in advance > > Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
On Fri, 2007-05-18 at 20:16 +0530, Shubha Vishwanath Karanth wrote:> Hi R,> My csv files are stored in the order, '1abc.csv', '2def.csv', > '3ghi.csv', '10files.csv' in a folder. When I read this into R from > list.files (R command: x=list.files("Z:/CSV/fold",full.names=F), I don't > get the same order, instead I get the order as "10files.csv" "1abc.csv" > "2def.csv" "3ghi.csv". But I don't want this ordering. So, how do I > maintain the oder which I have in my physical folder?> Thanks in advance > > Shubha>From ?list.files in the Value section:"The files are sorted in alphabetical order, on the full path if full.names = TRUE." Presumably you are on Windows and you have the folder view set to sort the files in some order, possibly by the date/time of creation? Check the folder settings to see how you have this set. In R the list of files is sorted in alpha order and in this case, the numbers are sorted based upon the order of the ASCII values of the numeric characters, not in numeric value order. You can try this approach using a regex and sub() to get the numeric value parts of the file names, get the ordered indices and then pass them back to the vector of file names:> Files[1] "10files.csv" "1abc.csv" "2def.csv" "3ghi.csv"> Files[order(as.numeric(sub("([0-9]*).*", "\\1", Files)))][1] "1abc.csv" "2def.csv" "3ghi.csv" "10files.csv" See ?sub, ?regex and ?order for more information. HTH, Marc Schwartz
Try this: library(gtools) mixedsort(x) On 5/18/07, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > My csv files are stored in the order, '1abc.csv', '2def.csv', > '3ghi.csv', '10files.csv' in a folder. When I read this into R from > list.files (R command: x=list.files("Z:/CSV/fold",full.names=F), I don't > get the same order, instead I get the order as "10files.csv" "1abc.csv" > "2def.csv" "3ghi.csv". But I don't want this ordering. So, how do I > maintain the oder which I have in my physical folder? > > > > Thanks in advance > > Shubha > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >