I don't feel like I have a superpowered laptop, but I got that code you sent to run within 2.166 seconds, with randomly generated values, not real ones of course. I'm not sure what is taking ten minutes to an hour with this code. Are you sure there are only ~60,000 values here, or is that the number of rows you are expecting?

In any case, I've replaced it with a somewhat vectorized version here. It's not the kind of code I would write, because I think it's a little odd you seem to be working with triples of values in the matrix - like columns 3, 6, 9, etc, mean something different from columns 4, 7, 10, etc - but that's perhaps another question. It's about 13.5x faster, reducing 2.16s to .16 seconds.

By the way, you can mark off code sections with ``` above the first and below the last line.

```
nrow <- 60000
ncol <- 98
final <- matrix(rnorm(rows * ncol), nrow = nrow, ncol = ncol)
# code you supplied
aa = matrix(NA, nrow = nrow(final), ncol=34)
st = Sys.time()
for (j in 1:32){
for (i in 1:nrow(final)){
if (final[i,3*j] > 1.5 & final[i,3*j+1] < 0.05 & final[i,3*j+2] < 0.05){
aa[i,j+2] = 1
} else if (final[i,3*j] < -1 & final[i,3*j+1] < 0.05 & final[i,3*j+2] < 0.05){
aa[i,j+2] = 2
}
}
}
ed = Sys.time()
ed-st
#> Time difference of 2.16 secs
vectorized <- matrix(NA, nrow = nrow(final), ncol = 32)
st <- Sys.time()
places_with_1 <- final[, seq(3, 96, by = 3)] > 1.5 & final[, seq(4, 97, by = 3)] < 0.05 & final[, seq(5, 98, by = 3)] < 0.05
places_with_2 <- final[, seq(3, 96, by = 3)] < -1 & final[, seq(4, 97, by = 3)] < 0.05 & final[, seq(5, 98, by = 3)] < 0.05
vectorized[places_with_1] <- 1
vectorized[places_with_2] <- 2
# attach two sets of NAs on the end
vectorized <- cbind(matrix(NA, nrow = nrow(final), ncol = 2), vectorized)
ed <- Sys.time()
ed - st
#> Time difference of 0.16 secs
# verify the results are the same:
identical(vectorized, aa)
#> TRUE
```

I'm happy to answer any follow-up questions you might have!