Joarder Kamal
Joarder Kamal

Reputation: 1397

How to get the global max value from a list of matrices containing NA values?

How to get the global max value if the matrices contain NA values? I tried with the given solution at

R: Getting maximum value from all matrices in a list

max(unlist(lapply(mylist,FUN=max)))

but the output is showing NA. Also tried with

max(unlist(lapply(na.omit(mylist),FUN=max)))

but still the output is NA.

To Reproduce:

m1 <- cbind(c(1,NA,3),c(7,2,4))
m2 <- cbind(c(-1,19,13),c(21,NA,5),c(3,3,0),c(4,5,6))
m3 <- cbind(c(1,2,3,NA,5),c(8,18,4,6,7))
mylist <- list(M1=m1,M2=m2,M3=m3)

Upvotes: 1

Views: 68

Answers (1)

rcs
rcs

Reputation: 68829

Just add na.rm=TRUE in the lapply statement:

max(unlist(lapply(mylist, FUN=max, na.rm=TRUE)))

or explicitly in the FUN argument:

max(unlist(lapply(mylist, FUN=function(x) max(x, na.rm=TRUE))))

Upvotes: 2

Related Questions