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)
}
CodePudding user response:
There are a few issues here.
- You want
seq_along()
, notseq()
.seq()
expects numeric arguments.
fileNumbers <- seq_along(fileNames)
vector()
defaults to creating a logical vector, but you want a character vector.
FileList<-vector(mode = “character”)
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.Your arguments are backwards:
append()
takes the object to be appended to, then the object to append.Finally, you can simplify your code by using
any()
instead ofsum() >= 1
, and by combining the twoif
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])
}