Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ignore NA in lapply?

Tags:

r

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 do not know which corresponds to which.For instance:res[[1]] represents 5, 3, or 1 in B? is there a way to name the output colms in t of its correspondence class (number) from B?

like image 306
bic ton Avatar asked Dec 21 '15 10:12

bic ton


1 Answers

We can use complete.cases

 lapply(re, function(x) {
         v1 <- c(t(do.call(cbind, x)))
         v1[complete.cases(v1)]})
like image 188
akrun Avatar answered Nov 05 '22 14:11

akrun