It's because those are character strings and they are sorted lexically
(i.e., alphabetically). I think you probably can get what you prefer
by using the mixedsort/mixedorder functions of the gtools package.
Take a look at this
x <- paste0("data",1:100, ".fit")
order(x)
sort(x)
library(gtools)
mixedorder(x)
mixedsort(x)
Best,
Michael
On Mon, May 28, 2012 at 10:06 AM, sam84 <samiyemny at yahoo.co.uk>
wrote:> The code given below worked well. However, the problem is that when I typed
> dir1 to see the results I found that R order the files as:
> [1] "data1.flt" ? "data10.flt" ?"data100.flt"
"data101.flt"
> ?[5] "data102.flt" "data103.flt"
"data104.flt" "data105.flt"
> ?[9] "data106.flt" "data107.flt"
"data108.flt" "data109.flt"
> ?[13] "data11.flt" ?"data110.flt"
"data111.flt" "data112.flt"
> ?[17] "data113.flt" "data114.flt"
"data115.flt" "data116.flt"
> .
> .
> to
> .
> .
> [357] "data91.flt" ?"data92.flt"
?"data93.flt" ?"data94.flt"
> [361] "data95.flt" ?"data96.flt"
?"data97.flt" ?"data98.flt"
> [365] "data99.flt"
>
>
> which will lead to wrong results.
> How to tell R to start reading from 1 to 365 in order.
> something like :
>
> [1] "data1.flt" ? "data2.flt" ?"data3.flt"
"data4.flt"
> not like:
>
> [1] "data1.flt" ? "data10.flt" ?"data100.flt"
"data101.flt"
> Here is the code:
> dir1<- list.files("C:\\Users\\Amin\\Desktop\\2001",
"*.flt", full.names > TRUE)
> results<- list()
> for (.files in seq_along(dir1)){
> ? ? ?file2 <- readBin(dir2[.files], double(), size = 4, n = w * 67420,
> signed = TRUE)
> ? ?results[[length(results) + 1L]]<- file1[file1 != -9999]*10}
> for (i in seq_along(results)){
> ? ?fileName <- sprintf("C:\\Users\\aalyaari\\Desktop\\New folder
> (2)\\NewFile%03d.bin", i)
> ? ?writeBin(as.integer(results[[i]]), fileName, size = 2)}
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Why-R-order-files-as-1-10-100-not-1-2-3-tp4631584.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.