我在多个文件列表中使用lapply函数。有没有办法我可以跳过当前文件上的函数而不返回任何内容,只是跳到文件列表中的下一个文件?
确切地说,我有一个检查条件的if语句,如果语句返回FALSE,我想跳到下一个文件。
我在多个文件列表中使用lapply函数。有没有办法我可以跳过当前文件上的函数而不返回任何内容,只是跳到文件列表中的下一个文件?
确切地说,我有一个检查条件的if语句,如果语句返回FALSE,我想跳到下一个文件。
lapply
将始终返回与列表长度相同的列表 X
提供。您可以简单地将项目设置为稍后可以过滤掉的项目。
例如,如果你有这个功能 parsefile
parsefile <-function(x) {
if(x>=0) {
x
} else {
NULL
}
}
然后你在向量上运行它 runif(10,-5,5)
result<-lapply(runif(10,-5,5), parsefiles)
然后你会让你的清单充满答案和 NULL
小号
你可以将其子集化 NULL
通过做...
result[!vapply(result, is.null, logical(1))]
正如其他人已经回答的那样,我不认为你可以继续下一次迭代而不使用 *apply
一系列功能。
在这种情况下,我使用Dean MacGregor的方法,只需要做一些小改动:我使用 NA
代替 NULL
,这使得过滤结果更容易。
files <- list("file1.txt", "file2.txt", "file3.txt")
parse_file <- function(file) {
if(file.exists(file)) {
readLines(file)
} else {
NA
}
}
results <- lapply(files, parse_file)
results <- results[!is.na(results)]
一个快速的基准
res_na <- list("a", NA, "c")
res_null <- list("a", NULL, "c")
microbenchmark::microbenchmark(
na = res_na[!is.na(res_na)],
null = res_null[!vapply(res_null, is.null, logical(1))]
)
说明了 NA
解决方案比使用的解决方案快得多 NULL
:
Unit: nanoseconds
expr min lq mean median uq max neval
na 0 1 410.78 446 447 5355 100
null 3123 3570 5283.72 3570 4017 75861 100
您可以定义要在呼叫中使用的自定义功能 lapply()
。下面是一些示例代码,它迭代文件列表并仅在名称不包含数字3时处理文件(有点人为,但希望这可以解决问题):
files <- as.list(c("file1.txt", "file2.txt", "file3.txt"))
fun <- function(x) {
test <- grep("3", x) // check for files with "3" in their name
if (length(test) == 0) { // replace with your statement here
// process the file here
}
// otherwise do not process the file
}
result <- lapply(files, function(x) fun(x)) // call lapply with custom function