R - loop and break after value found for each Rows

此生再无相见时 提交于 2021-02-08 08:00:13

问题


I have a matrix of 0-1. What I would like to do is to loop into this matrix and search for the 1. Each time a 1 has been found, to simply jump or pass that row, in order to only record 1 value per row.

What I am trying to know if the first episode of the sequence is a 1. I was thinking there might be a solution with

break 

But I am unsure how to use it properly.

So this is my first matrix

SoloNight = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 0, 0), .Dim = 10:11)

This is my empty matrix - to record the 1.

matSolo = matrix(0, nrow = nrow(SoloNight), ncol = ncol(SoloNight) ) 

This is my attempt to loop

for(i in 1:nrow(matSolo)){
  for(j in 1:ncol(matSolo)){
    if(SoloNight[i,j] == 1) break
    {matSolo [i,j] <- 1}
  }
}

How can I break after finding the value 1 for each rows ?

Any suggestion how I could do that ?

(Expected matrix)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
 [1,]    0    0    0    0    0    0    0    0    0     0     0
 [2,]    0    0    0    0    0    0    0    0    0     0     0
 [3,]    0    0    0    0    0    0    0    0    0     0     0
 [4,]    0    0    0    0    0    0    0    0    0     0     0
 [5,]    0    0    0    0    0    0    0    0    0     0     0
 [6,]    0    0    0    0    0    0    0    0    0     0     0
 [7,]    0    0    0    0    0    0    0    0    0     0     0
 [8,]    0    0    0    1    0    0    0    0    0     0     0
 [9,]    0    1    0    0    0    0    0    0    0     0     0
[10,]    0    0    0    0    0    0    0    0    0     0     0

回答1:


You seem to be fond of for loops and those seem like a natural choice here. Just change your code to this:

for(i in 1:nrow(matSolo)){
  for(j in 1:ncol(matSolo)){
    if(SoloNight[i,j] == 1) {
      matSolo [i,j] <- 1
      break
    }
  }
}

However, this will be quite slow for big matrices. Fortunately, it's easy to translate to Rcpp:

library(Rcpp)
cppFunction(
'IntegerMatrix firstOne(const IntegerMatrix mat) {
  IntegerMatrix res(mat.nrow(), mat.ncol());
  for (int i=0; i<mat.nrow(); i++) {
    for (int j=0; j<mat.ncol(); j++) {
      if (mat(i,j) == 1) {
        res(i,j) = 1;
        break;
      }
    }
  }
  return res;
}
')

firstOne(SoloNight)



回答2:


You can also try

library(matrixStats)    
matSolo <- 1*(rowCumsums(SoloNight)==1)



回答3:


You may try

 indx <- max.col(SoloNight, 'first')*(rowSums(SoloNight)!=0)
 matSolo[cbind(1:nrow(matSolo), indx) ] <- 1

Benchmarks

  set.seed(24)
  m1 <- matrix(sample(c(0,1), 5000*5000, replace=TRUE), ncol=5000)

  khashaa <- function(){
      matSolo <- rowCumsums(m1)
      matSolo[matSolo!=1] <- 0
       }
  khashaa2 <- function(){matSolo <- 1*(rowCumsums(m1)==1)}

  roland <- function() firstOne(m1)

  akrun <- function() {
       indx <- max.col(m1, 'first')*(rowSums(m1)!=0)
        matSolo <- m1*0
        matSolo[cbind(1:nrow(matSolo), indx) ] <- 1
       }

   system.time(akrun())
   #user  system elapsed 
   #0.349   0.044   0.395 
   system.time(roland())
   # user  system elapsed 
   # 0.144   0.021   0.166 
   system.time(khashaa())
   # user  system elapsed 
   # 0.555   0.055   0.611 
   system.time(khashaa2())
   # user  system elapsed 
   #0.265   0.054   0.319 

 library(microbenchmark)
 microbenchmark(akrun(), khashaa(),khashaa2(), roland(), unit='relative', times=20L)
 #Unit: relative
 #    expr      min       lq     mean   median       uq      max neval  cld
 # akrun() 2.383404 2.453934 2.298975 2.283178 2.304992 1.993665    20   c 
 #khashaa() 3.542353 3.601232 3.420879 3.429367 3.476025 2.898632    20    d
 #khashaa2() 1.900968 2.029436 1.909676 1.923457 1.948551 1.693583    20  b  
 # roland() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000    20 a   


来源:https://stackoverflow.com/questions/30966577/r-loop-and-break-after-value-found-for-each-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!