簡體   English   中英

R - 如何/是否將 mapply 用於嵌套循環?

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

我試圖弄清楚如何避免使用嵌套的 for 循環或 lapply 來處理嵌套列表。 如何調整下面的 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() 

這可以解決問題:

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

或者你想要更多?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM