I am using a lapply function over a list of multiple files. Is there a way in which I can skip the function on the current file without returning anything and just skip to the next file in the list of the files?
To be precise, I have an if statement that checks for a condition, and I would like to skip to the next file if the statement returns FALSE.
lapply
will always return a list the same length as the X
it is provided. You can simply set the items to something that you can later filter out.
For example if you have the function parsefile
parsefile <-function(x) {
if(x>=0) {
x
} else {
NULL
}
}
Edit: {
As Florent Angly shows, you should replace NULL
with NA
}
and you run it on a vector runif(10,-5,5)
result<-lapply(runif(10,-5,5), parsefile)
then you'll have your list filled with answers and NULL
s
You can subset out the NULL
s by doing...
result[!vapply(result, is.null, logical(1))]