I want to output a subset of a list of pathnames, conditional on characters in the list elements (prefix in the filename). I can get a for loop
to work, but I want to do it using sapply
because I'm guessing it's a better approach.
The for loop
works; the result is a list of pathnames with filenames starting with 500 or higher. Sapply
does not work; i
does not iterate as I'm expecting, but there might be other problems appending the element to the new list.
# -----------------------------------------
# add pathnames to files that have a prefix of less than 500,
# to a new list
# -----------------------------------------
# make list
# myList <- list.files(path = "D:/test", pattern = "*.txt")
myList <- list("D:/test/472_a.txt", "D:/test/303_b.txt", "D:/test/500_a.txt", "D:/test/505_b.txt", "D:/test/700_a.txt")
# preallocate subsetted list
myListSubset <- vector("list", length = length(myList))
# -----------------------------------------
# for loop - this works
# -----------------------------------------
for (i in 1:length(myList)) {
print(paste("i is", i))
print(paste(i, "element of myList is", myList[i]))
swath <- str_sub(basename(paste(myList[i], collapse = "")), 1, 3)
# only add swaths ge to 500 to the subsetted list
if (swath >= 500) {
print(paste("swath #", swath))
myListSubset[[i]] <- paste(myList[i], collapse = "")
}
}
# remove Null elements
print(myListSubset)
myListSubset[sapply(myListSubset, is.null)] <- NULL
print(myListSubset)
# -----------------------------------------
# -----------------------------------------
# sapply - this does not work
# -----------------------------------------
i <- 1
sapply(myList, function(s){
swath <- str_sub(basename(s), 1, 3) # swath is the 1st 3 digits in file name
print(paste("swath #", swath))
if (swath >= 500) {
print(paste("list element is", s, "and the class is", class(s)))
print(paste("i is", i, "and the class is", class(i)))
myListSubset[[i]] <- s
i <- (i + 1)
print(paste("i is", i))
}
}
)
# remove Null elements
print(myListSubset)
myListSubset[sapply(myListSubset, is.null)] <- NULL
print(myListSubset)
# -----------------------------------------