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?
We can use complete.cases
lapply(re, function(x) {
v1 <- c(t(do.call(cbind, x)))
v1[complete.cases(v1)]})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With