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

  • SEARCH
  • Home
  • 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 9160071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:38:55+00:00 2026-06-17T13:38:55+00:00

I have a directory called specdata that contains csv files (such as 001.csv,002.csv,…,332.csv). Now

  • 0

I have a directory called “specdata” that contains csv files (such as 001.csv,002.csv,…,332.csv). Now I want my function to read all the files in this directory and return a data.frame where the first column is the name of the file and the second column is the number of complete cases.

for example:

id nobs
1  108
2  345
...
etc

Now, I wrote this function that reads all the files in “specdata” directory and generates the sum of complete cases in each file. But I do not know how to put each no. generated by “nobs” from the loop into the new data.frame in this format:

id  nobs
1   108
2   345
...
...
332 16

My function:

complete <- function(directory, id = 1:332) {

for(i in 1:332)
  {
    if(i<10)
      {

      path<-paste(directory,"/00",id[i],".csv",sep="")
      }
    if(i>9 & i<100)
      {

      path<-paste(directory,"/0",id[i],".csv",sep="") 
      }
    if(i>99 & i<333)
      {

      path<-paste(directory,"/",id[i],".csv",sep="") 
      }  

    mydata<-read.csv(path)
    #nobs<-nrow(na.omit(mydata))
    nobs<-sum(complete.cases(mydata))

  }


}

the problem is that “nobs” dynamically gets created row-by-row in the for loop and I want to put the entire list of “nobs” for all the files into data.frame. I have tried lot of ways but am unable to put entire list of “nobs” into the data.frame along with the “id” numbers.

Can someone please suggest a way to return the data.frame in requested order?

  • 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-17T13:38:56+00:00Added an answer on June 17, 2026 at 1:38 pm

    The simplest way to build up a list of all the nobs values goes something like this:

    complete <- function(directory, id = 1:332) {
      # Create an empty vector outside the for loop
      nobs_vector <- c()
      for(i in 1:332)
      {
        if(i<10)
        {
          path<-paste(directory,"/00",id[i],".csv",sep="")
        }
        if(i>9 & i<100)
        {
          path<-paste(directory,"/0",id[i],".csv",sep="") 
        }
        if(i>99 & i<333)
        {
          path<-paste(directory,"/",id[i],".csv",sep="") 
        }  
    
        mydata<-read.csv(path)
        #nobs<-nrow(na.omit(mydata))
        nobs<-sum(complete.cases(mydata))
        # Add the value to the end of the vector
        nobs_vector <- c(nobs_vector, nobs)
      }
      # Take a look at the final vector you end up with
      print(nobs_vector)
    }
    

    It’s not necessarily that elegant or efficient, but it does get you those values in a form that persists after the for loop is done. If you wanted to build up a dataframe in a similar way, have a look at ?rbind

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one directory called images/tips. Now in that directory I have many images
I have a directory called public_html/hammer/ (my CMS). I want to use the image
i have a directory called testDir and it contains 1000 file, some of them
I have a file called middle.config that is deployed in the same directory as
Consider I have a directory called root that has two directories: x and y
I have a folder in my Application directory called Commands.folder. What I want to
I have a directory called /user/local/ inside which i have several files of the
I have a directory called project . It contains two sub-directories called client and
For my site I have a directory called /test/ . I want to rewrite
Scenario: I have a directory on a server that hosts my website that contains

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.