function that reads a directory full of files and reports the number of completely observed cases in each data file

Please help. I am trying to create a function that reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases.

This is the code that I have created but it does not work. The answer under nobs should not be 0

complete <- function(directory, id = 1:332) {
  files <- list.files(directory, full.names = TRUE)
  nobs = numeric()
  for (i in id) {
    nobs <- sum(complete.cases(files))
  }
  return(data.frame(id, nobs))
}
    
The output the I get is :

> complete("specdata", 3)
  id nobs
1  3    0
> 

id is not being incremented, so complete only returns the last value in the loop. Initialize id with zero outside the loop and increment it each trip through the loop.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.