A function that counts the number of complete cases

Please help. I am trying to write 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 my code and the output it produces:

complete <- function(directory, id = 1:332) {
  files <- list.files(directory, full.names = TRUE)
  nobs = numeric()

  for (i in 1:length(id)) {
    nobs <- sum(complete.cases(files[i]))
  return(data.frame(id, nobs))

This is the output:

complete("specdata", 25:30)
  id nobs
1 25    0
2 26    0
3 27    0
4 28    0
5 29    0
6 30    0

But the code is not counting the completed cases. Please help

I have a few questions:

  • What language is this?
  • What defines a “complete case”?

This looks like… R? Woah, I wasn’t expecting to be seeing that here.

to help out @Jeff_Hanna, the language is R. It’s mainly used for data science and statistics. Complete cases refers to the number of rows in a data frame (or other container type) that don’t have any missing values.

I really don’t know the language very well, but right off the bat I notice 2 possible issues with the logic.

  1. You’re not actually reading any data from files, you’re running complete.cases directly on the filename itself, which I’d assume is not what you’re after. You might need to use read.table or something like that.
  2. Your for loop is always iterating starting from 1, so while id might be 25:30, you’re actually checking files 1:6. That also doesn’t seem right to me. I would assume that you’d want to do for (i in id) there, but I could be wrong.
1 Like