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:
[1] 101
[1] 0
[1] 30
[1] 3
[1] 0
The sum function expects numbers. If you provide sum with logicals they get converted to numbers.
Think about it …
The mean function expects numbers. If you provide mean with logicals they get converted to numbers.
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
##########################################################################.
#-----------------------------------------------------------------------------------.
# ANSWER: (a) write a command to show the number of students who got an 80 or above.
#-----------------------------------------------------------------------------------.
# EXAMPLE 1
grades = c(85, 80, 79, 65, 55, 72, 90, 95)
sum ( grades >= 80 )
[1] 4
# Original: sum ( grades >= 80 )
# : sum ( c(85, 80, 79, 65, 55, 72, 90, 95) >= 80 )
# : sum ( c(85>=80, 80>=80, 79>=80, 65>=80, 55>=80, 72>=80, 90>=80, 95>=80))
# : sum ( c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE))
# : sum ( c(1,1,0,0,0,0,1,1))
# 4
# EXAMPLE 2
grades = c(45, 78, 78, 85, 88)
sum ( grades >= 80 )
[1] 2
#-------------------------------------------------------------------------------------.
# ANSWER: (b) write a command to show the percent of the class who got an 80 or above
#-------------------------------------------------------------------------------------.
# EXAMPLE 1
grades = c(85, 80, 79, 65, 55, 72, 90, 95)
# YOUR CODE GOES HERE # 0.5
# ONE WAY
mean( grades >= 80 )
[1] 0.5
# Original: mean ( grades >= 80 )
# : mean ( c(85, 80, 79, 65, 55, 72, 90, 95) >= 80 )
# : mean ( c(85>=80, 80>=80, 79>=80, 65>=80, 55>=80, 72>=80, 90>=80, 95>=80))
# : mean ( c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE))
# : mean ( c(1,1,0,0,0,0,1,1))
# 0.5
# ANOTHER WAY
sum( grades>= 80) / length(grades)
[1] 0.5
#-------------------------------------------------------------------------------------.
# ANSWER: (c) show the grades that were 80 or above
#-------------------------------------------------------------------------------------.
# EXAMPLE 1
grades = c(85, 80, 79, 65, 55, 72, 90, 95)
# ANSWER
grades[grades>=80] # 85 80 90 95
[1] 85 80 90 95
# Original: grades[grades>=80]
# grades[c(85, 80, 79, 65, 55, 72, 90, 95)>=80]
# grades[c(85>=80, 80>=80, 79>=80, 65>=80, 55>=80, 72>=80, 90>=80, 95>=80)]
# grades[c(85>=80, 80>=80, 79>=80, 65>=80, 55>=80, 72>=80, 90>=80, 95>=80)]
# : grades[c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE)]
# 85 80 90 95
# EXAMPLE 2
grades = c(45, 78, 78, 85, 88)
# ANSWER
grades[grades>=80] # 85 88
[1] 85 88
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
13.5.1 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:
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:
[1] 100
[1] 100
[1] 200
[1] 200
[1] 200
[1] 100
[1] 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
grades = c(60,70,80,90,100)
# The following uses a logical index to extract those values
# that correspond to the TRUE values in the index.
grades[c(FALSE, TRUE, FALSE, TRUE, TRUE)] # 70 90 100
[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.
grades[c(0,1,0,1,1)] # 60 60 60
[1] 60 60 60
What’s going to be displayed by the following? Why?
[1] 60 60
# EXPLANATION - A vector must contain all the same type of data.
# If you try to mix logical and numeric values in the same vector then
# Logical values are converted to numeric (TRUE becomes 1, FALSE becomes 0).
# Therefore, this selects the first and fourth elements from the vector.
# A zero index is ignored.
© 2025 Y. Rosenthal. All rights reserved.