Re: [R] sort file names in numerical order
Hi Yes it is possible- one way is: fileNames[order(sprintf(%02s, sub([[:upper:]],, fileNames)))] [1] A1 B1 A2 B2 A10 B10 Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England ARMIDALE NSW 2351 Email home: mac...@northnet.com.au At 06:47 18/07/2010, you wrote: I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Greetings. I see that you've gotten an elegant solution to your problem. I've appended a poor-man's solution, which I generated more for my own edification than for yours. -- Mike ## modified file names, changed to exhibit sorting on letters fileNames - c(A10, B10, A1, A2, B1, B2); fileNames ## use regular expressions to pick off letters, numbers fnLet - gsub((^[^0-9]+).*$, \\1, fileNames); fnLet fnNum - gsub(^[^0-9]+(.*)$, \\1, fileNames); fnNum ## need to force numeric sorting of the numbers fnNum - as.numeric(fnNum) ## find the order of the numbers, for later use in subsetting fnNumOrd - order(fnNum); fnNumOrd ## pack all the relevant information into a data frame, then select from that fnPieces - data.frame(fnLet, fnNum, fileNames, stringsAsFactors=FALSE); fnPieces ## partial sort by numbers only (gives new df) dfPartialSort - fnPieces[fnNumOrd, ]; dfPartialSort ## find the order of the names, then select from new df with that fnLetOrd - order(dfPartialSort[, 1]); fnLetOrd dfFullSort - dfPartialSort[fnLetOrd, ]; dfFullSort ## the file names have gone along for the ride, so pick them off now sortedFileNames - dfFullSort$fileNames; sortedFileNames __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] sort file names in numerical order
Another option: require(gtools) ?mixedsort mixedsort(fileNames) [1] A1 A2 A10 B1 B2 B10 -- David On Jul 18, 2010, at 5:16 AM, Duncan Mackay wrote: Hi Yes it is possible- one way is: fileNames[order(sprintf(%02s, sub([[:upper:]],, fileNames)))] [1] A1 B1 A2 B2 A10 B10 Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England ARMIDALE NSW 2351 Email home: mac...@northnet.com.au At 06:47 18/07/2010, you wrote: I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Greetings. I see that you've gotten an elegant solution to your problem. I've appended a poor-man's solution, which I generated more for my own edification than for yours. -- Mike ## modified file names, changed to exhibit sorting on letters fileNames - c(A10, B10, A1, A2, B1, B2); fileNames ## use regular expressions to pick off letters, numbers fnLet - gsub((^[^0-9]+).*$, \\1, fileNames); fnLet fnNum - gsub(^[^0-9]+(.*)$, \\1, fileNames); fnNum ## need to force numeric sorting of the numbers fnNum - as.numeric(fnNum) ## find the order of the numbers, for later use in subsetting fnNumOrd - order(fnNum); fnNumOrd ## pack all the relevant information into a data frame, then select from that fnPieces - data.frame(fnLet, fnNum, fileNames, stringsAsFactors=FALSE); fnPieces ## partial sort by numbers only (gives new df) dfPartialSort - fnPieces[fnNumOrd, ]; dfPartialSort ## find the order of the names, then select from new df with that fnLetOrd - order(dfPartialSort[, 1]); fnLetOrd dfFullSort - dfPartialSort[fnLetOrd, ]; dfFullSort ## the file names have gone along for the ride, so pick them off now sortedFileNames - dfFullSort$fileNames; sortedFileNames __ R-help@r-project.org 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. __ R-help@r-project.org 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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.
[R] sort file names in numerical order
Hello, I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Kind regards, Sebastian __ R-help@r-project.org 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.
Re: [R] sort file names in numerical order
library(gtools) ?mixedorder --- On Sat, 7/17/10, Sebastian Gibb li...@sebastiangibb.de wrote: From: Sebastian Gibb li...@sebastiangibb.de Subject: [R] sort file names in numerical order To: r-help@r-project.org Received: Saturday, July 17, 2010, 4:31 AM Hello, I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Kind regards, Sebastian __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] sort file names in numerical order
On Sat, Jul 17, 2010 at 4:31 AM, Sebastian Gibb li...@sebastiangibb.de wrote: Hello, I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? The code for mixsort which does that type of sorting can be found on this page: http://gsubfn.googlecode.com and gtools has a mixedsort function. __ R-help@r-project.org 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.
Re: [R] sort file names in numerical order
thanks a lot, it works. you wrote: library(gtools) ?mixedorder --- On Sat, 7/17/10, Sebastian Gibb li...@sebastiangibb.de wrote: From: Sebastian Gibb li...@sebastiangibb.de Subject: [R] sort file names in numerical order To: r-help@r-project.org Received: Saturday, July 17, 2010, 4:31 AM Hello, I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Kind regards, Sebastian __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] sort file names in numerical order
I get some file names by list.files(). These names are in alphabetical order. I want to change it to logical numeric order. Example: fileNames - c(A10, A1, A2, B1, B2, B10) sort(fileNames) [1] A1 A10 A2 B1 B10 B2 I want to have: A1 A2 A10 B1 B2 B10 Is this possible? Greetings. I see that you've gotten an elegant solution to your problem. I've appended a poor-man's solution, which I generated more for my own edification than for yours. -- Mike ## modified file names, changed to exhibit sorting on letters fileNames - c(A10, B10, A1, A2, B1, B2); fileNames ## use regular expressions to pick off letters, numbers fnLet - gsub((^[^0-9]+).*$, \\1, fileNames); fnLet fnNum - gsub(^[^0-9]+(.*)$, \\1, fileNames); fnNum ## need to force numeric sorting of the numbers fnNum - as.numeric(fnNum) ## find the order of the numbers, for later use in subsetting fnNumOrd - order(fnNum); fnNumOrd ## pack all the relevant information into a data frame, then select from that fnPieces - data.frame(fnLet, fnNum, fileNames, stringsAsFactors=FALSE); fnPieces ## partial sort by numbers only (gives new df) dfPartialSort - fnPieces[fnNumOrd, ]; dfPartialSort ## find the order of the names, then select from new df with that fnLetOrd - order(dfPartialSort[, 1]); fnLetOrd dfFullSort - dfPartialSort[fnLetOrd, ]; dfFullSort ## the file names have gone along for the ride, so pick them off now sortedFileNames - dfFullSort$fileNames; sortedFileNames __ R-help@r-project.org 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.