How to optimize the removal of rows in an array?

6

I have an array of dimensions:

> dim(filtro1)
[1] 2806519      31

I need to remove rows from this array that meet a condition. So far so good. However, the computationally looping for this has been very expensive (time consuming - 8 to 10 hours). I've tried rbind, but this is slower than the solution below:

for(i in 1:length(filtro1[,1])){
  if(filtro1[i,31] == 0){
    filtro1 <- filtro1[-i,]
    print(i)
  }
}
  • print (i) is just for me to follow the loop execution

I tried running the code in parallel with foreach and% dopar%, but apparently it does not work because the reasoning above depends on the index

Does anyone know how to do the removal of array rows faster and more efficiently?

    
asked by anonymous 22.12.2018 / 12:52

1 answer

5

R is a vectored language and the best way to do this would be something like:

filtro1 <- filtro1[filtro1[,31] != 0, ]

I think the best place to learn about vectorization is the chapter 3 of R Inferno .

    
22.12.2018 / 12:59