How to ignore NA?

set.seed(123) B = matrix( c(5, 3, 3, 1, 5, 1,3,1,NA,NA), nrow=5, ncol=2) m1<-matrix(nrow=5,ncol=2,data=runif(10)) m2<-matrix(nrow=5,ncol=2,data=runif(10)) m2[1,2]=NA; ml <- list(m1, m2) ind <- sapply(unique(c(B)), function(x) which(B == x, arr.ind = TRUE)) re <- lapply(ind, function(x) lapply(ml, function(y) y[x])) res=lapply(re, function(x) c(t(do.call(cbind, x)))) 

but I don’t know what corresponds to what. For example: res[[1]] represents 5, 3, or 1 in B ? is there a way to name the output columns in t your correspondence class (number) from B?

+5
source share
1 answer

We can use complete.cases

  lapply(re, function(x) { v1 <- c(t(do.call(cbind, x))) v1[complete.cases(v1)]}) 
+5
source

Source: https://habr.com/ru/post/1238819/


All Articles