Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:56:01+00:00 2026-06-04T11:56:01+00:00

Possible Duplicate: R sorts a vector on its own accord – bad boy! How

  • 0

Possible Duplicate:
R sorts a vector on its own accord – bad boy!
How can I read the files in a directory in sorted order using R?

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(I even used sort(dir1) but didn’t sort them).
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", "*.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:\\New folder (2)\\NewFile%03d.bin", i)
    writeBin(as.integer(results[[i]]), fileName, size = 2)
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:56:03+00:00Added an answer on June 4, 2026 at 11:56 am

    If you know the structure of the name of your files you could use that instead of grabbing them directly from dir.

    for(i in seq(365)){
        filename <- paste0("data", i, ".flt")
        # Do some stuff with filename
    }
    

    paste0 was introduced in R 2.15 so for older versions you’ll need:

    for(i in seq(365)){
        filename <- paste("data", i, ".flt", sep = "")
        # Do some stuff with filename
    }
    

    Further edits because you seem to be confused. filename gets updated each iteration through the loop. You can see this by printing the filename each time.

    for(i in seq(365)){
        filename <- paste("data", i, ".flt", sep = "")
        print(filename)
        # Do some stuff with filename
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ struct sorting Is it possible to sort a vector in C++
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Regex Testing Tools What sorts of tools do other programmers use when
Possible Duplicate: Private/protected inheritance Can you give me example for private inheritance in C++
Possible Duplicate: Interview Q: sorting an almost sorted array (elements misplaced by no more
Possible Duplicate: Application Color schemes Is there a tutorial of sorts that covers which
Possible Duplicate: Using Perl, how can I sort an array using the value of
Possible Duplicate: Sort multidimensional array by multiple keys I want know how I can
Possible Duplicate: How can I sort arrays in php by a custom alphabet? For
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.