简体   繁体   English

R - 如何/是否将 mapply 用于嵌套循环?

[英]R - how/whether to use mapply for a nested loop?

I'm trying to figure out how to avoid using a nested for loop or lapply to deal with a nested list.我试图弄清楚如何避免使用嵌套的 for 循环或 lapply 来处理嵌套列表。 How can I tweak the mapply function below or use another base r function to extract the TRUE value I am looking for?如何调整下面的 mapply 函数或使用另一个基本 r 函数来提取我正在寻找的 TRUE 值?

#data
l = list(a = list(2, 3, NA, 5, 1), b = list(4, 3, 3, 5, 2), c = list(5, 1, 3, 2, 4))

#how can I avoid a nested lapply
lapply(l, function(y){
  lapply(y, function(x){
    is.na(x)
  })
}) %>%
  unlist() %>%
  any()

#my attempt - I am getting the result I want but I beleive this is the incorrect implementation
mapply(function(x,y) is.na(x), l) %>%
  unlist() %>%
  any() 

This would do the trick:这可以解决问题:

any(is.na(unlist(l))) 

Or do you want more?或者你想要更多?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM