【发布时间】:2019-12-21 13:29:36
【问题描述】:
在for loop 中,可以使用next() 跳到下一个迭代。如example:
# skip 3rd iteration and go to next iteration
for(n in 1:5) {
if(n==3) next
cat(n)
}
在将我的函数应用于对象列表时,我想做类似的事情。有点像这样:
l <- c(1:2, NA, 4:5)
myfun <- function(i){
if(is.na(i)) next
message(paste('test',i))
}
lapply(l, myfun)
有没有办法根据条件跳过 lapply 中的特定值?
【问题讨论】:
-
怎么样 -
lapply(na.omit(l), myfun)? -
我会在使用
lapply之前过滤l。如果想知道异常发生在哪里,可以使用?tryCatch函数。