Ad

Loop To List Names Of Files With NAs

- 1 answer

I have a folder called "Holder" with a list of files in it. I am trying to write a loop that will iterate through the list of files, and add to a vector (called "FileList") the names of files that contain NAs. When I check my statement "sum(is.na(Orig))>=1" on a file that I know has an NA, it returns "TRUE". However, when I source the script, my vector returns "logical(0)". What am I missing here?

setwd("/Users/.../Desktop/NAs")
fileNames <- Sys.glob("*.csv")
fileNumbers <- seq(fileNames)
FileList<-vector()

for (fileNumber in fileNumbers) {
  Orig <- read.csv(fileNames[fileNumber],
                       header = TRUE,
                       sep = ",")
    
  if (sum(is.na(Orig))>=1) {
    append(fileNames[fileNumber], FileList)
  }}

Also tried the below, with same result:

  if (sum(is.na(Orig$D_postIAT))>=1) {
    append(fileNames[fileNumber], FileList)
  } else if (sum(is.na(Orig$D_preIAT))>=1) {
    append(fileNames[fileNumber], FileList)
  }
Ad

Answer

There are a few issues here.

  1. You want seq_along(), not seq(). seq() expects numeric arguments.
fileNumbers <- seq_along(fileNames)
  1. vector() defaults to creating a logical vector, but you want a character vector.
FileList<-vector(mode = “character”)
  1. You need to assign the results of each append(). With very few exceptions (outside of R6 classes), objects in R aren’t modified in place.

  2. Your arguments are backwards: append() takes the object to be appended to, then the object to append.

  3. Finally, you can simplify your code by using any() instead of sum() >= 1, and by combining the two if clauses into one with an or. So try:

if (any(is.na(Orig$D_postIAT)) || any(is.na(Orig$D_preIAT))) {
    FileList <- append(FileList , fileNames[fileNumber])
  }
Ad
source: stackoverflow.com
Ad