Reputation: 1043
I have a nested R list with the following structure:
my_list <- list(a = list(value = 1, alert = FALSE), b = list(value = 2, alert = FALSE), c = list(value = 3, alert = TRUE))
Is there logic to determine whether all the elements named alert
are FALSE? For example:
ifelse(<all mylist["alerts"] are FALSE>, print("No alerts to report!"), print("ALERT"))
> ALERT
Upvotes: 0
Views: 51
Reputation: 19088
You can try this with sapply
In the general case
ifelse(all(sapply(my_list, function(x) x["alert"]==F)),
"No alerts to report!","ALERT")
[1] "ALERT"
Using the logical values directly as pointed out by @Ritchie Sacramento
ifelse(any(sapply(my_list, function(x) !x["alert"][[1]])),
"ALERT", "No alerts to report!")
[1] "ALERT"
Upvotes: 1
Reputation: 25323
With data.table::rbindlist
:
c("NO ALERT", "ALERT")[1+1*any(data.table::rbindlist(my_list)$alert)]
#> [1] "ALERT"
Upvotes: 0