3 > 2
[1] TRUE
c(100,200) < c(50,400) # FALSE TRUE
[1] FALSE TRUE
c(1,2,3,4) <= c(1,2) # recycling rule: c(1,2,3,4) <= c(1,2,1,2)
[1] TRUE TRUE FALSE FALSE
Remember that “indexing a vector” means to use one vector to identify the values that you want from anther vector. The “index” (which is a vector) gets placed in the [square brackets], e.g. someVector [ theIndex ]
So far we learned two different types of indexes (a) positive numbers, e.g. someVector[c(1,3)] gets only the 1st and 3rd values. (b) negative numbers, e.g. someVector[c(-2,-4,-6)] gets all values EXCEPT for the 2nd, 4th and 6th values.
In this section we cover a new way to index, ie. using logical values as the index e.g. someVector[c(TRUE, FALSE, TRUE)] - this is explained below.
Remember that a “logical vector” is contains only the values TRUE and/or FALSE. See examples below:
3 > 2
[1] TRUE
c(100,200) < c(50,400) # FALSE TRUE
[1] FALSE TRUE
c(1,2,3,4) <= c(1,2) # recycling rule: c(1,2,3,4) <= c(1,2,1,2)
[1] TRUE TRUE FALSE FALSE
You can index any vector by using a logical vector to identify which values you want.
For example assume that x is a vector. The expression x[c(TRUE, FALSE, TRUE)]
will return the 1st and 3rd values from x but not the 2nd value (see examples below). In general the values returned from the vector outside the [square brackets] are the values in those positions that correspond to the TRUE values in the vector inside the [square brackets].
# an example
<- c(70, 72, 80, 85, 88, 95)
grades
grades
[1] 70 72 80 85 88 95
c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE)] # 70 72 95 grades[
[1] 70 72 95
As usual, the value of the grades vector doesn’t change unless you assign a new value to it (which we did not do in this example).
# grades didn't change grades
[1] 70 72 80 85 88 95
# another example
c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE)] # 85 88 grades[
[1] 85 88
Remember that a command that involves more than one vector is often affected by R’s “recycling rule”. In this case, the recycling rule works to repeat the logical vector as many times as necessary to equal the length of the vector being indexed. See examples below.
<- c(70, 72, 80, 85, 88, 95)
grades grades
[1] 70 72 80 85 88 95
c(TRUE, FALSE)] # 70 80 88 (i.e. every other grade starting with 1st grade) grades[
[1] 70 80 88
# original : grades[c(TRUE, FALSE)]
# recycling rule: grades[c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)]
# : 70 80 88
# 70 72 80 85 88 95 grades
[1] 70 72 80 85 88 95
c(FALSE, TRUE)] # 72 85 95 (every other grade starting with 2nd grade) grades[
[1] 72 85 95
# original : grades[c(FALSE, TRUE)]
# recycling rule: grades[c(FALSE, TRUE, FALSE, TRUE, FALSE, TRUE)]
# : 72 85 95
# 70 72 80 85 88 95 grades
[1] 70 72 80 85 88 95
c(TRUE, FALSE, TRUE, TRUE)] # 70 80 85 88 grades[
[1] 70 80 85 88
# original : grades[c(TRUE, FALSE, TRUE, TRUE)]
# recycling rule: grades[c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE)]
# : 70 80 85 88
We can generate a logical vector with a comparison operator (i.e. > < >= <= == !=).
<- c(70, 72, 80, 85, 88, 95)
grades grades
[1] 70 72 80 85 88 95
>= 80 # FALSE FALSE TRUE TRUE TRUE TRUE grades
[1] FALSE FALSE TRUE TRUE TRUE TRUE
# original : grades >= 80
# : c(70,72,80,85,88,95) >= 80
# recycling rule : c(70,72,80,85,88,95) >= c(80,80,80,80,80,80)
# : c(70>=80 , 72>=80 , 80>=80 , 85>=80 , 88>=80 , 95>=80)
# : c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
# : FALSE FALSE TRUE TRUE TRUE TRUE
>= c(80, 90) # FALSE FALSE TRUE FALSE TRUE TRUE grades
[1] FALSE FALSE TRUE FALSE TRUE TRUE
# original : grades >= c(80, 90)
# : c(70,72,80,85,88,95) >= c(80,90)
# recycling rule : c(70,72,80,85,88,95) >= c(80,90,80,90,80,90)
# : c(70>=80,72>=90,80>=80,85>=90,88>=80,95>=90)
# : c(FALSE, FALSE ,TRUE ,FALSE ,TRUE ,TRUE)
Often we want to get only those values from a vector which match a specific condition. To do so use a logical vector that represents that condition as the [index].
For example, if we want to see all the grades that are 80 or above. We use a logical vector INSIDE the brackets that represents the condition - see the examples below.
<- c(70, 72, 80, 85, 88, 95)
grades grades
[1] 70 72 80 85 88 95
# EXAMPLE: Display only those grades that are 80 or greater
>= 80 ] # 80 85 88 95 grades[ grades
[1] 80 85 88 95
# original : grades[ grades >= 80 ]
# create the logical vector : grades[ c(FALSE,FALSE,TRUE,TRUE,TRUE,TRUE)]
# extract the grades that are in TRUE positions: 80 85 88 95
# ANOTHER EXAMPLE: Show grades that are above average.
# 70 72 80 85 88 95 grades
[1] 70 72 80 85 88 95
> mean(grades)] # 85 88 95 grades[grades
[1] 85 88 95
# original : grades[grades > mean(grades)]
# mean : grades[grades > 81.66667 ]
# logical vector: grades[c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)]
# : 85 88 95
#################################################################.
# QUESTION: grades is a vector that contains student grades, e.g.
#
# grades <- c(70, 72, 80, 85, 88, 95)
#
# (a) Write a command that will only show those grades that are
# an even multiple of 10 (i.e. that end in zero).
# HINT - think about using the %% operator as part of your
# answer.
#
# (b) Write a command that displays all the other grades.
#################################################################.
###########################################################################.
# QUESTION: Write a command to display the values in the grades vector
# that are at least 5 points above average
#
#
# EXAMPLE 1:
# > grades <- c(70, 72, 80, 85, 88, 95) # average is 81.6667
# > YOUR CODE GOES HERE
# [1] 88 95
#
#
# EXAMPLE 2:
# > grades <- c(50, 70, 72, 80, 85, 88, 95) # average is 77.143
# > YOUR CODE GOES HERE
# [1] 85 88 95
#########################################################################.
#########################################################################.
# QUESTION: Show grades that are within 5 points of the highest grade
# HINT: use max function and a little math
#
#
# EXAMPLE 1:
# > grades <- c(70, 72, 80, 85, 91, 93, 93, 95)
# > YOUR CODE GOES HERE
# [1] 91 93 93 95
#
#
# EXAMPLE 2:
# > grades <- c(95, 92, 89, 89, 88, 80, 75)
# > YOUR CODE GOES HERE
# [1] 95 92
#
#
# EXAMPLE 3:
# > grades <- c(95, 89, 89, 88, 80, 75)
# > YOUR CODE GOES HERE
# [1] 95
#########################################################################.