8 Subsetting and Logical Operators
8.1 Indexing
Indexing is used to examine or assign the value of an object.
<- c(3,7,11,19)
x # Assignment
c(2,3)] <- c(120,140)
x[
# Examine
<- c(3,7,11,19)
x 3]
x[4] x[
Extend the utility of indexing: []
by combining with c
. For example to get the second and third element in the variable “x”
<- c(3,7,11,19)
x 1]
x[2]
x[c(1,2)]
x[c(2,1)]
x[<- c(3,2,1)
y x[y]
8.2 Indexing using a negative sign
# Extend the index using a negative sign
<- c(1,2,3,4,5,6)
y.vect -3]
y.vect[-c(2,3]] y.vect[
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(1:20, byrow = TRUE, nrow = 5)
matrix_c
matrix_cdim(matrix_c)
# this will be a good time to practice using the ":", seq, rep
# matrix_c[row,col]
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_cc(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]]
matrix_c[
# Some complexity
1:2, 1:3][c(1,2),2] matrix_c[
8.4 Indexing Data Frames
<- data.frame(students = c("Angie", "Kelly", "Adam", "Jessica"),
student.df grades = rep("A+", 4),
GRE.Percentiles = runif(4, min = 0.70, max = 0.99))
$students
student.df$students[4]
student.df$students[c(4)]
student.df$students[c(1,4)] student.df
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
<- c(2,3,4)
y <- c(3,7,9,2)
w > 1
y == 2
w < 4 | w >= 8
w > 4 & w <= 8 w
Combine logical operators with the function which
# logical operators examples
<- c(2,3,4)
y > 1
y which(y > 1)
which(y < 3)
which(y <= 3)
which(y <= 3)] <- 0 y[
<- data.frame(students = c("Angie", "Kelly", "Adam", "Jessica"),
student.df grades = rep("A+", 4),
GRE.Percentiles = c(0.8, 0.83, 0.74, 0.96),
Sat.scores = c(1212, 907, 979, 988))
student.df$Sat.scores > 1000
student.df
# Use this for indexing
$Sat.scores[student.df$Sat.scores > 1000]
student.df$Sat.scores > 1000, ]
student.df[student.df
$Sat.scores > 1000
student.df$GRE.Percentiles > 0.75
student.df
# Combine boolean values using and, or
$Sat.scores > 1000 & student.df$GRE.Percentiles > 0.75
student.df<- student.df$Sat.scores > 1000 | student.df$GRE.Percentiles > 0.75
bool.ind 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 |