TRUE + 100 # interprets as 1 + 100
[1] 101
FALSE * 100 # interprets as 0 * 100
[1] 0
30 + FALSE # interprets as 30 + 0
[1] 30
FALSE + TRUE + TRUE + FALSE + TRUE # interprets as 0+1+1+0+1
[1] 3
If R sees a logical value where it expects a numeric value then R will automatically interpret the logical value as a numeric value in the following way
In the following example, the arithmetic operators (e.g. +,-,*,/,%%,%/%,^) expect that both of their “operands” will be numbers. However, in the following examples one or both of the operands are TRUE or FALSE. Therefore, R interprets TRUE as a 1 and FALSE as a 0 (zero). See the examples:
TRUE + 100 # interprets as 1 + 100
[1] 101
FALSE * 100 # interprets as 0 * 100
[1] 0
30 + FALSE # interprets as 30 + 0
[1] 30
FALSE + TRUE + TRUE + FALSE + TRUE # interprets as 0+1+1+0+1
[1] 3
=10
x=15
y>y) * 100 # result is 0 * 100, or just 0 (x
[1] 0
# EXPLANATION:
#
# step 1: x>y is 10>15. This produces FALSE
# step 2: the * requires numbers so the FALSE is
# converted to a number. R converts FALSE to 0.
= 200
x = 0.3
y >y) * 100 # result is 1 * 100, or just 100 (x
[1] 100
# EXPLANATION:
#
# step 1: x>y is 200>0.3. This produces TRUE
# step 2: the * requires numbers so the TRUE must be
# converted to a number. R converts TRUE to 1.
The sum function expects numbers. If you provide sum with logicals they get converted to numbers.
sum ( c( FALSE, TRUE, TRUE, FALSE) ) # same as sum(0,1,1,0)
[1] 2
Think about it …
The mean function expects numbers. If you provide mean with logicals they get converted to numbers.
mean ( c( TRUE, FALSE, TRUE, TRUE) ) # same as mean(1,0,1,1)
[1] 0.75
Think about it …
##########################################################################.
# QUESTION: grades is a vector that contains grades of students on a test.
#
# (a) write a command to show the number of students who got an 80 or above.
#
# (b) write a command to show the percent of the class who got an 80 or above
#
# (c) show the grades that were 80 or above
##########################################################################.
##########################################################################.
# QUESTION: Add up the grades that are above 80
##########################################################################.
To convert numeric values to logical values, R uses slightly different rules.
If R sees a numeric value where it expects a logical value (i.e. TRUE or FALSE) then R will automatically interpret the numeric value as a logical (i.e. TRUE or FALSE) value in the following way
Quick review of ifelse(LOGICAL_VALUE, SOME_VALUE, ANOTHER_VALUE)
Recall that the ifelse function expects a logical value (i.e. TRUE or FALSE) as the first argument. The following are some simple examples:
= 10 x = 3 y ifelse(x>y, 100, 200) # 100
[1] 100
= 10 x = 15 y ifelse(x>y, 100, 200) # 200
[1] 200
ifelse(c(x>y, x>100), "apple", "pear") # 1st argument has 2 logical values
[1] "pear" "pear"
When the ifelse function contains numbers in the first argument, then the numbers are converted into logicals using the rules stated above (0 becomes FALSE; non-zero becomes TRUE). See the following examples:
ifelse(97, 100, 200) # 97 is converted to TRUE, result is 100
[1] 100
ifelse(-3.2, 100, 200) # -97 is also converted to TRUE, result is 100
[1] 100
ifelse(0, 100, 200) # 0 is converted to FALSE, result is 200
[1] 200
=0
x=100
y
ifelse(x, 100, 200) # ifelse(0,100,200) same as ifelse(FALSE, 100, 200)
[1] 200
ifelse(x*y, 100, 200) # ifelse(100*0,100,200) same as ifelse(FALSE, 100, 200)
[1] 200
ifelse(y+x, 100, 200) # ifelse(100+0,100,200) same as ifelse(TRUE, 100, 200)
[1] 100
# This one is tricky.
ifelse(TRUE+FALSE*FALSE, 100, 200)
[1] 100
# EXPLANATION:
# original: ifelse(TRUE+FALSE*FALSE, 100, 200)
# becomes: ifelse(1+0*0, 100, 200)
# order of ops: ifelse(1+0, 100, 200)
# ifelse(1, 100, 200)
# final result: 100
Remember that an index can be a numeric vector or a logical vector. Therefore, TRUE / FALSE vectors will NOT be converted to 1’s and 0’s in an index.
# Start with some data
= c(60,70,80,90,100)
grades
# The following uses a logical index to extract those values
# that correspond to the TRUE values in the index.
c(FALSE, TRUE, FALSE, TRUE, TRUE)] # 70 90 100 grades[
[1] 70 90 100
# The following uses a numeric index. Here the first value
# is extracted a few times. An index of 0 is ignored.
c(0,1,0,1,1)] # 60 60 60 grades[
[1] 60 60 60
What’s going to be displayed by the following? Why?
= c(60,70,80,90,100)
grades c(1, 0, 0, 1)] grades[
= c(60,70,80,90,100)
grades c(1, FALSE, FALSE, TRUE)] grades[
c( TRUE, 999, FALSE )
sum(c(TRUE, FALSE, TRUE, TRUE))
mean(c(TRUE, FALSE, TRUE, TRUE))