Assume I have the following list:
list(c(1:5,NA,NA),NA,c(NA,6:10))
[[1]]
[1] 1 2 3 4 5 NA NA
[[2]]
[1] NA
[[3]]
[1] NA 6 7 8 9 10
I want to replace all NA
s with 0
:
[[1]]
[1] 1 2 3 4 5 0 0
[[2]]
[1] 0
[[3]]
[1] 0 6 7 8 9 10
I was originally thinking is.na
would be involved, but couldn't get it to affect all list elements. I learned from the related question (Remove NA from list of lists), that using lapply
would allow me to apply is.na
to each element, but that post demonstrates how to remove (not replace) NA
values.
How do I replace NA
values from multiple list elements?
I've tried for
loops and ifelse
approaches, but everything I've tried is either slow, doesn't work or just plain clunky. There's got to be a simple way to do this with an apply
function...