0

I did loop with if statement as you can see in the code

for (j in  2:4){
  for (i in 1:5-j){
    if (B[i,5-j]>10){   
        A[i,j] = 0
    }else{
       A[i,j] = 2
    }
  }
}

But it doesn´t work.

In if (B[i, 5 - j] > 10) { the condition has length > 1 and only the first element will be used.

I don't know how it works fine.

qdot
  • 6,195
  • 5
  • 44
  • 95
liguang
  • 161
  • 1
  • 9
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. And is `A` and `B` in this case? Maybe you just need `i in 1:(5-j)`? Not sure what the intention is there. – MrFlick Jan 30 '18 at 15:38
  • 2
    @Cath not only maybe. This error is due to lack of parenthesis in `for (i in 1:5-j){ `,where `1:5-j` for `j=2` turns to `[1] -1 0 1 2 3` rather than `1:(5-j)` and hence `[1] 1 2 3` for `j=2`. – nadizan Jan 30 '18 at 15:52

0 Answers0