I have a standard 2x2 table
Yes No
Yes a b
No c d
I want to create a condition whereby IF(a or b or c or d = 0) then 0.5 is added on to each of the cells a,b,c,d.
I have tried this:
if(a && b && c && d == 0){
a=a+0.5, b=b+0.5, c=c+0.5, d=d+0.5
}
But I am getting an error saying
Error: unexpected ',' in:
"if(a && b && c && d== 0){
a=a+0.5,"
i.e. I don't think it is letting me put multiple things to execute.
Also I don't think that the &&
is right between each of the letters as I believe that means IF(a and b and ...)
UPDATE TO QUESTION:
I have another related question.
If I have say a set of say n tables, all in the format:
Yes No
Yes a b
No c d
and if one of the a,b,c or d in any of the n tables is equal to zero then 0.5 is added on to each of the a,b,c,d for all of the n tables. How would I do that?
My list looks like the following:
n11 n12 n21 n22
1 188 1157 173 1168
2 2 201 1 101
3 369 2280 354 2289
4 1 61 0 61
5 1306 16870 1333 16773
6 4 81 3 79
7 6 117 5 118
8 19 334 15 318
9 1 49 0 48
10 0 36 1 33
11 2 114 3 113
12 13 433 37 696
13 1 64 0 65
14 4 157 1 160
15 1 42 0 43
16 1 150 5 146
17 7 1124 10 1117
18 2 78 2 77
and what I am trying to say is that if any of the aspects of the cells of the table are 0, then I want 0.5 to be added on to every cell.
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.
You can automate the evaluation of logical tests by NESTING the AND, OR, NOT functions inside a single IF function. This means that if we have multiple conditions but we want to return a single output, we can nest any of the conjunction functions inside an IF and specify outputs accordingly.
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.
The problem is with the commas that separate your variables. R syntax does not allow you to do it. Write it this way:
if (a && b && c && d == 0){
a=a+0.5
b=b+0.5
c=c+0.5
d=d+0.5
}
Another problem is that the behaviour you described does not match with your code. If you write && it means and, not or. If you want to check if each element is equal to 0, you should write the following:
Modified based on Rodrigo's comment, the correct code would be:
if (0 %in% c(a,b,c,d)){
a=a+0.5
b=b+0.5
c=c+0.5
d=d+0.5
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With