R:lapply函数 - 跳过当前函数循环 [英] R: lapply function - skipping the current function loop

查看:452
本文介绍了R:lapply函数 - 跳过当前函数循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个文件列表上使用lapply函数。有没有办法可以跳过当前文件的功能而不返回任何内容,只需跳到文件列表中的下一个文件?



准确地说,我有一个if语句来检查条件,如果语句返回FALSE,我想跳到下一个文件。

解决方案

lapply将始终返回一个长度与 X 提供。例如,如果你有函数 parsefile ,你可以简单地将这些项目设置为可以稍后过滤掉的项目。



  parsefile <-function(x){
if(x> = 0){
x
} else {
NULL
}
}

,然后在vector上运行 runif(10,-5,5)

  result< -lapply(runif(10,-5,5),parsefiles)

那么你会得到你的清单充满了答案和 NULL s



您可以将 NULL 通过做...

 结果[!vapply(result,is.null ,logical(1))] 


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
  }
}

and you run it on a vector runif(10,-5,5)

result<-lapply(runif(10,-5,5), parsefiles)

then you'll have your list filled with answers and NULLs

You can subset out the NULLs by doing...

result[!vapply(result, is.null, logical(1))]

这篇关于R:lapply函数 - 跳过当前函数循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆