# Statistical Methods in Medical Research - # Laaketieteellisen tutkimuksen tilastolliset menetelmat # At University of Helsinki # 5.8.2025 # Matti Pirinen ### ### 3. Learn R: Logical comparisons and 'if'-statement ### # We can assess whether a statement is true and R returns one of # two possible logical values: TRUE or FALSE 1 > 2 # is 1 greater than 2? 1 < 2 # is 1 smaller than 2? 1 > 1 # is 1 greater than 1? 1 >= 1 # is 1 greater or equal to 1? 1 <= 1 # is 1 less or equal to 1? x = 1 # this is an assignment not a comparison! -- from now on x has value 1 x == 2 # this is a comparison: Is x equal to 2? Note the '=' symbol is twice in the comparison operator! x != 2 # is x NOT equal to 2? x == 1 # is x equal to 1? x != 1 # is x NOT equal to 1? # To combine several comparisons we can use "and" or "or". # These are denoted by symbols '&' for AND and '|' for OR # To check whether 'x' is in interval (0,1) we can ask whether # 'x' is both >0 and <1 (x > 0) & (x < 1) # If we allow the upper limit to be exactly 1 then the statement becomes TRUE since currently x equals 1: (x > 0) & (x <= 1) # To check whether at least one of these is true: # 'x' is > 0 or -2 < x < -1, (x > 0) | (x > -2 & x < -1) # If we increase the first comparison point from 0 to 1, # then the statement becomes FALSE because currently x equals 1 (x > 1) | (x > -2 & x < -1) # We can use such logical TRUE/FALSE values to control what R does. if(x > 0) { print("First IF was true.") x = x + 1 } if(x <= 0) { print("Second IF was true.") x = x - 1 } # If the comparison within the 'if()' statement is TRUE, # then the code within the following block '{ }' is run. # If it is FALSE, then the code block within '{ }' will be skipped. # Above, only the first IF is true, so R changes x from 1 to 2 = 1 + 1, but # since the second IF is not true, R does not subtract 1 from x at the second step. # (Note also how the print("") command has only been run for the first if-statement that was true.) # So currently x should be 2. x # Note that the curly brackets don't need to be on their own lines so this works as well if(x <= 0) {x = x - 1} # But if you have more than one expression within the brackets, # then it is good to use a separate line for each command. # Define a vector of 3 positive and 2 negative values x = c(1, -2, 4, -10, 1) # If we apply a logical comparison to the vector, # the result is an element-wise comparison x < 0 # We can ask which indexes of the vector correspond to value TRUE in comparison which(x < 0) #returns 2 and 4 # We can apply mathematical operations, like sum() or mean(), to logical values # and then R will replace TRUE with 1 and FALSE with 0. # So sum(x < 0) will compute how many elements of x are < 0 sum(x < 0) # and mean(x < 0) is the same as sum(x < 0)/length(x), that is, the proportion of TRUEs. mean(x < 0) #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*# # Test Yourself 3.1. (Answers are at the end of this file.) #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*# # BMI of 6 individuals are # 24.5, 38, 27.4, 30.4, 26.3, 17.5 #(a) Make a vector of these 6 BMI values #(b) Use logical comparisons to determine which individuals # have BMI value within interval (20,25), i.e., 20 < BMI < 25. #(c) Apply sum() to the comparison in (b) to determine how many are in the interval (20,25). #(d) Apply mean() to the comparison in (b) to determine which proportion is in the interval (20,25). #(e) Use logical comparisons to determine which individuals # have either (BMI > 30) or (BMI < 20). #(f) Set value of variable x to 3. Write an if-sentence that increases # value of variable x by +1 in case the value of x is < 4. # Show the value of 'x' after running the if-sentence. # Rerun the if-sentence and show again the value of 'x'. # Note that it should change only in the first round. # ## ### ANSWERS ## # # BMI of 6 individuals are # 24.5, 38, 27.4, 30.4, 26.3, 17.5 #(a) Make a vector of these 6 BMI values bmi = c(24.5, 38, 27.4, 30.4, 26.3, 17.5) #(b) Use logical comparisons to determine which individuals # have BMI value within interval (20,25), i.e., 20 < BMI < 25. (bmi > 20) & (bmi < 25) which((bmi > 20) & (bmi < 25)) #show indexes #(c) Apply sum() to the comparison in (b) to determine how many are in the interval (20,25). sum( (bmi > 20) & (bmi < 25) ) #(d) Apply mean() to the comparison in (b) to determine which proportion is in the interval (20,25). mean( (bmi > 20) & (bmi < 25) ) #(e) Use logical comparisons to determine which individuals # have either (BMI > 30) or (BMI < 20). (bmi > 30) | (bmi < 20) which((bmi > 30) | (bmi < 20)) #(f) Set value of variable x to 3. Write an if-sentence that increases # value of variable x by +1 in case value of x is < 4. # Show the value of 'x' after running the if-sentence. # Rerun the if-sentence and show again the value of 'x'. # Note that it should change only in the first round. x = 3 x #x is 3 if( x < 4) { x = x + 1} x #x increased to 4 if( x < 4) { x = x + 1} x #x is still 4