简体   繁体   中英

R - change matrix values based on another matrix indices

I have two matrices:

m1 <- matrix(runif(750), nrow = 50, byrow=T)
m2 <- matrix(rep(TRUE,750), nrow = 50, byrow=T)

For each m1 row, I need to find the indices of the two lowest values. Then, I need to use the remaining indices (ie not the two lowest values) to assign FALSE in m2 .

It is fairly easy to do for one row:

ind <- order(m1[1,], decreasing=FALSE)[1:2]
m2[1,][-ind] <- FALSE

Therefore, I can use a loop to do the same for all rows:

for (i in 1:dim(m1)[1]){
  
  ind <- order(m1[i,], decreasing=FALSE)[1:2]
  m2[i,][-ind] <- FALSE
  
}

However, in my data set this loop runs slower than I would like (since my matrices are quite large - 500000x150000).

Is there any faster, R way to achieve the same result without the use of loops?

你可以试试下面的代码

m2 <- t(apply(m1,1,function(x) x %in% head(sort(x),2)))

You can try apply since you have matrix :

val <- rep(TRUE, ncol(m1))
m3 <- t(apply(m1, 1, function(x) {val[-order(x)[1:2]] <- FALSE;val}))

你可以做:

m2 <- t(apply(m1, 1, function(x) rank(x)<3))

Using pmap

library(purrr)
pmap_dfr(as.data.frame(m1), ~ min_rank(c(...)) < 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM