# Statistical Methods in Medical Research -
# Laaketieteellisen tutkimuksen tilastolliset menetelmat
# At University of Helsinki
# 3.9.2022
# Matti Pirinen
###
### 3. Learn R: Logical comparisons and IF statement
###
# We can assess whether a statement holds 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 comparison!
x == 2 #this is comparison: Is x equal to 2? Note the '=' symbol is there twice.
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 comparison 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 upper limit of =1 then statement becomes TRUE since currently x equals 1:
(x > 0) & (x <= 1)
# To check whether 'x' is x > 0 or -2 < x < -1 we do a comparison
(x > 0) | (x > -2 & x < -1)
# If we increase first comparison point from 0 to 1,
# then statement becomes FALSE because currently x equals 1
(x > 1) | (x > -2 & x < -1)
# We can use such values to control what R does.
if(x > 0) {
x = x + 1
}
if(x <= 0) {
x = x - 1
}
# If the comparison within if() statement is TRUE,
# then the code in the following block '{ }' is run.
# If it is FALSE, then the code block in '{ }' will be skipped.
# Above, only the first IF is true, so R changes x from 1 to 2 = 1 + 1, but
# since second IF is not true, R does not subtract 1 from x at the second step.
# 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 good to use different lines.
# 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 elementwise 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 --> 1 and FALSE --> 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
# are normal weight, say, 20 < BMI < 25.
#(c) Apply sum() to the comparison in (b) to determine how many are normal weight.
#(d) Apply mean() to the comparison in (b) to determine which proportion is normal weight.
#(e) Use logical comparisons to determine which individuals
# are either obese (BMI > 30) or underweight (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.
#
##
### 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
# are normal weight, say, 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 normal weight.
sum( (bmi > 20) & (bmi < 25) )
#(d) Apply mean() to the comparison in (b) to determine which proportion is normal weight.
mean( (bmi > 20) & (bmi < 25) )
#(e) Use logical comparisons to determine which individuals
# are either obese (BMI > 30) or underweight (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