8 Subsetting and Logical Operators

8.1 Indexing

Indexing is used to examine or assign the value of an object.

x <- c(3,7,11,19)
# Assignment
x[c(2,3)] <- c(120,140)

# Examine
x <- c(3,7,11,19)
x[3]
x[4]

Extend the utility of indexing: [] by combining with c. For example to get the second and third element in the variable “x”

x <- c(3,7,11,19)
x[1]
x[2]
x[c(1,2)]
x[c(2,1)]
y <- c(3,2,1)
x[y]

8.2 Indexing using a negative sign

# Extend the index using a negative sign
y.vect <- c(1,2,3,4,5,6)
y.vect[-3]
y.vect[-c(2,3]]

8.3 Slicing Matrices

A matrix function in R is a 2-dimensional array that has m number of rows and n number of columns. In other words, matrix in R programming is a combination of two or more vectors with the same data type.

# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow =  TRUE 
matrix_c <-matrix(1:20, byrow = TRUE, nrow = 5)
matrix_c
dim(matrix_c)

# this will be a good time to practice using the ":", seq, rep
# matrix_c[row,col]
matrix_c[1:3, 1:2]
matrix_c[rep(1,3), seq(from = 1, to = 2)]
matrix_c[c(1,1,1), seq(from = 1, to = 2)] <- NA

matrix_c <-matrix(1:20, byrow = TRUE, nrow = 5)
matrix_c
matrix_c[c(1,4), seq(from = 1, to = 2)] <- NA
matrix_c

matrix_c <-matrix(1:20, byrow = TRUE, nrow = 5)

matrix_c[seq(1,nrow(matrix_c)), seq(1,c(ncol(matrix_c)))[-3]]

# Some complexity
matrix_c[1:2, 1:3][c(1,2),2]

8.4 Indexing Data Frames

student.df <- data.frame(students = c("Angie", "Kelly", "Adam", "Jessica"),
                         grades = rep("A+", 4), 
                         GRE.Percentiles = runif(4, min = 0.70, max = 0.99))

student.df$students
student.df$students[4]
student.df$students[c(4)]
student.df$students[c(1,4)]

8.5 Logical operators

Symbol Operation
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== exactly equal to

Logical operators are useful in indexing and subsetting data classes. They return TRUE and FALSE values.

# logical operators examples
y <- c(2,3,4)
w <- c(3,7,9,2)
y > 1
w == 2
w < 4 | w >= 8
w > 4 & w <= 8

Combine logical operators with the function which

# logical operators examples
y <- c(2,3,4)
y > 1
which(y > 1)
which(y < 3)
which(y <= 3)
y[which(y <= 3)] <- 0
student.df <- data.frame(students = c("Angie", "Kelly", "Adam", "Jessica"),
                         grades = rep("A+", 4), 
                         GRE.Percentiles = c(0.8, 0.83, 0.74, 0.96), 
                         Sat.scores = c(1212, 907, 979, 988))
student.df
student.df$Sat.scores > 1000

# Use this for indexing
student.df$Sat.scores[student.df$Sat.scores > 1000]
student.df[student.df$Sat.scores > 1000, ]

student.df$Sat.scores > 1000
student.df$GRE.Percentiles > 0.75

# Combine boolean values using and, or
student.df$Sat.scores > 1000 & student.df$GRE.Percentiles > 0.75
bool.ind <- student.df$Sat.scores > 1000 | student.df$GRE.Percentiles > 0.75
student.df[bool.ind, ]

Logical operators practice

Number Exercise
1. Assign a vector a to have six elements: 1,0,8,11,12,20
2. Which elements in a are less than five
3. Which elements in a are greater than 13 or less than 10