ravi
ravi

Reputation: 1088

Optimize nested for loop in r

I have following r code. It has nested for loops. I want to replace the number with NA if the i+3 row value is zero. It works fine for small datasets, however, for large datasets, it hangs. I am assuming the nested for loops are not the efficient way to achieve it. Could someone help suggest enhancing the code, preferably tidyverse library?

x <- data.frame(c1=c(1,2,3,2,1,3),
                c2=c(4,5,6,2,3,4),
                c3=c(7,8,9,7,1,6),
                c4=c(4,0,9,1,5,0),
                c5=c(3,8,0,7,3,6),
                c6=c(2,8,5,0,5,7),
                row.names = c("r1","r2","r3","r4","r5","r6"))

for( i in 1:nrow(x)){
  for(j in 1:3){
    if (x[i, j+3] == 0){
      x[i, j] <- NA
    }
  }
}

Output: x

   c1 c2 c3 c4 c5 c6
r1  1  4  7  4  3  2
r2 NA  5  8  0  8  8
r3  3 NA  9  9  0  5
r4  2  2 NA  1  7  0
r5  1  3  1  5  3  5
r6 NA  4  6  0  6  7

Upvotes: 3

Views: 606

Answers (1)

akuiper
akuiper

Reputation: 214987

The loop over rows is not necessary, you can vectorize the outer loop with ifelse:

x[1:3] <- lapply(1:3, function(n) ifelse(x[[n+3]] == 0, NA, x[[n]]))
x
#   c1 c2 c3 c4 c5 c6
#r1  1  4  7  4  3  2
#r2 NA  5  8  0  8  8
#r3  3 NA  9  9  0  5
#r4  2  2 NA  1  7  0
#r5  1  3  1  5  3  5
#r6 NA  4  6  0  6  7

Or simpler method you can modify the first three columns based on the last three columns by doing boolean indexing and assignment:

x[1:3][x[4:6] == 0] <- NA
x
#   c1 c2 c3 c4 c5 c6
#r1  1  4  7  4  3  2
#r2 NA  5  8  0  8  8
#r3  3 NA  9  9  0  5
#r4  2  2 NA  1  7  0
#r5  1  3  1  5  3  5
#r6 NA  4  6  0  6  7

Upvotes: 7

Related Questions