R - how/whether 将 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)))
或者你想要更多?
我想知道如何避免使用嵌套 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)))
或者你想要更多?