IF “OR” multiple conditions

In R you can’t use , to separate line, but you can use ;.

Also, the way you are doing considers a,b and c are boolean (TRUE/FALSE), which is not the case as they are numbers. Your condition should be :

if (a == 0 || b == 0 || c == 0 || d == 0)

Note that your code will run nevertheless, even if a,b and c are not boolean since they are numbers and there is an equivalence between FALSE and a == 0. This means you could also write your condition as :

if (!a || !b || !c || !d)

For the UPDATE, I consider matList is the list of matrices :

for (ii in  1:length(matList())) {
    if (any(matList[[ii]] == 0)) {  
        matList = lapply(matList, function(X){X+0.5})
        break # Exit the for loop
    }
}

lapply applies mat + 0.5 (i.e + 0.5 to each element of the matrix thanks to R sugar) to every element (here matrices) of the list matList and returns the resulting list.

Leave a Comment

tech