9  Vectors

9.1 Questions

  • What are vectors, and why are they essential in R?

  • What are the different types of vectors and their key characteristics?

  • How does one create a vector?

  • What operations can be performed on a vector?

  • How do I create and access different elements within a vector?

  • Can I combine and manipulate vectors to achieve specific tasks?

  • What are some common functions for working with vectors?

9.2 Learning Objectives

  • Define and create vectors in R.

  • Understand how to perform basic operations on vectors.

  • Understand the concept of vectors as fundamental data structures in R.

  • Master the creation and manipulation of vectors using various techniques.

  • Identify and differentiate between different types of vectors based on their elements.

  • Utilize functions and operators to combine, subset, and transform vectors with ease.

  • Apply your understanding of vectors to enhance your R coding and data analysis efficiency.

9.3 Lesson Content

9.3.1 Introduction

A vector is a collection of elements of the same data type, and they are a basic data structure in R programming.

Vectors cannot be of mixed data type. The most common way to create a vector is with c(), where “c” stands for combine. In R, vectors do not have dimensions; therefore, they cannot be defined by columns or rows. Vectors can be divided into atomic vectors and lists (discussed in the “Data Structures” {sec-data-structure-1} section). The atomic vectors include logical, character, and numeric (integer or double).

Additionally, R is a vectorized language because mathematical operations are applied to each element of the vector without the need to loop through the vector.

Examples of vectors made up of different data types are shown below:

  • Numbers
c(2, 10, 16, -5)
  • Characters
c("R", "RStudio", "Shiny", "Quarto")
  • Logicals
c("TRUE", "FALSE", "TRUE")

9.3.2 Sequence Generation

To generate a vector with a sequence of consecutive numbers, we can use: sequence(), or seq().

Generate a sequence using :

a <- 9:18
a
a_rev <- 18:9
a_rev
a_rev_minus <- 5:-3
a_rev_minus

Generate a sequence using sequence()

b <- sequence(7)
b
c <- sequence(c(5,9))
c

Generate a sequence using seq()

The seq() function has four main arguments: seq(from, to, by, length.out), where “from” and “to” are the starting and ending elements of the sequence. Additionally, “by” is the difference between the elements, and “length.out” is the maximum length of the vector.

d <- seq(2,20,by=2)
d
f <- seq(2,20, length.out=5)
f
h <- seq(20,2,by=-2)
h
j <- seq(20, 2, length.out=3)
j

The seq() function can also be used with the arguments first, by, and length.

seq(first, by = ___, length = ____)

Example

seq(3, by = 4, length = 30)

Repeating vectors

To create a repeating vector, we can use rep().

rep(number, times)

k <- rep(c(0,3,6), times = 3)
k

rep(range, times)

l <- rep(2:6, each = 3)
l

rep(number or range, length.out)

m <- rep(7, length.out = 20)
m
n <- rep(7:10, length.out = 20)
n

Random number generation

The runif() function can be used to generate a specific number of random numbers between a minimum and maximum value.

runif(n, min = 0, max = 1)

runif(5, min = 3, max = 100)

NOTE: If you run the previous code block multiple times you will get different answers. Use set.seed() to get a similar sequence every time you run the calculation.

set.seed(12345)
runif(5, min = 3, max = 100)

9.3.3 More functions to manipulate vectors

Generate a random vector with 50 elements

set.seed(54321)
sam <- runif(50, min = 3, max = 100)
sam

Sample

sample(sam, size = 15)

To get the same sample repeatedly

set.seed(1234)
sample(sam, size = 15)
sort(sam)
rev(sort(sam))

Create two vectors and look at the similarities/differences between the two

set.seed(2468)
vec_top_1 <- seq(2, by = 6, length = 100)
vec_top_2 <- seq(4, by = 4, length = 100)
vec_top_1 
vec_top_2
union(vec_top_1, vec_top_2)
intersect(vec_top_1, vec_top_2)
setdiff(vec_top_1, vec_top_2)

9.3.4 Vector Operations

Vectors of equal length can be operated on together. If one vector is shorter, it will get recycled, as its elements are repeated until it matches the elements of the longer vector. When using vectors of unequal lengths, it would be ideal if the longer vector is a multiple of the shorter vector.

  1. Basic Vector Operations
vec_1 <- 1:10
vec_1 * 12 # multiplication
vec_1 + 12 # addition
vec_1 - 12 # subtraction
vec_1 / 3 # division
vec_1^4 # power
sqrt(vec_1) # square root
  1. Operations on vectors

Operations on vectors of equal length Additionally, we can perform operations on two vectors of equal length.

  1. Create two vectors
vec_3 <- 5:14
vec_3
vec_4 <- 12:3
vec_4
  1. Perform various arithmetic operations
vec_3 + vec_4
vec_3 - vec_4
vec_3 / vec_4
vec_3 * vec_4
vec_3 ^ vec_4
  1. Functions that can be applied to vectors

The functions listed below can be applied to vectors:

  1. any()

  2. all()

  3. nchar()

  4. length()

  5. typeof()

Examples

any(vec_3 > vec_4)
any(vec_3 < vec_4)
all(vec_3 > vec_4)
all(vec_3 < vec_4)
length(vec_3)
length(vec_4)
typeof(vec_3)
typeof(vec_4)

Determine the number of letters in a character

vec_5 <- c("R", "RStudio", "Shiny", "Quarto")
nchar(vec_5)
  1. Vectors and mathmatical/statistical operations

A variety of mathematical operations can be performed on vectors. These operations together with examples are listed below.

  1. Create a new vector
stat_math_vec_1 <- seq(2, by = 6, length = 120)
stat_math_vec_1
  1. Perform mathematical operations on the vector
  • Length
length(stat_math_vec_1)
  • Sum
sum(stat_math_vec_1)
  • Minimum
min(stat_math_vec_1)
  • Maximum
max(stat_math_vec_1)
  • Mean
mean(stat_math_vec_1)
  • Median
median(stat_math_vec_1)
  • Quantile
quantile(stat_math_vec_1)
  • Standard Deviation
sd(stat_math_vec_1)
  • Inter-Quartile Range (IQR)
IQR(stat_math_vec_1)
  • Cumulative Sum
cumsum(stat_math_vec_1)
  • Range
range(stat_math_vec_1)
  • Cut This function cuts a range of values into bins and specifies labels for each bin.

The argument “breaks” can be the number of bins, or it can be specific break points.

cut(stat_math_vec_1, breaks = 6)

cut(stat_math_vec_1, breaks = c(0, 120, 240, 360, 480, 600, 720))
  • Pretty

This function creates a sequence of equally spaced values that are rounded to the closest integer.The argument “n” lists the number of intervals.

pretty(stat_math_vec_1)
pretty(stat_math_vec_1, n = 20)
  • Trigonometric functions
sin(stat_math_vec_1)
cos(stat_math_vec_1)
tan(stat_math_vec_1)
  • Logarithmic functions
log(stat_math_vec_1)
log2(stat_math_vec_1)
log10(stat_math_vec_1)
  1. Miscellaneous functions
  1. Changing the case of a character vector
char_vec <- c("a", "b", "c", "d")
char_vec_upper <- toupper(char_vec)
char_vec_upper
char_vec_lower <- tolower(char_vec_upper)
char_vec_lower
  1. Recycling of vectors
vec_3 + c(10, 20)
vec_3 + c(10, 20, 30) 
# Will result in a warning as the longer vector is not a multiple of the shorter one
  1. Accessing elements of a vector and subsetting

To access the elements of a vector, we can use numeric-, character-, or logical-based indexing.

Examples

  1. Name the columns of a vector with names().
  1. Create the vector.
vec_name <- 1:5
vec_name
  1. Name the individual elements.
names(vec_name) <- c("a", "c", "e", "g", "i")
vec_name
  1. Use the vector index to filter
vec_index <- 1:5
vec_index
  1. Logical vector as an index
vec_index[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
  1. Filter vector based on an index
vec_index[1:3]
  1. Access a vector using its position
vec_index[4]

With negative indexing, every element except the one specified is returned.

vec_index[-2]
vec_index[c(2,4)]
  1. Modify a vector using indexing
vec_index
vec_index[5] <- 1000
vec_index
  1. Create matrices by combining vectors by row and column
  1. Create four new vectors
vec_mat_df_1 <- seq(2,20, length.out=10)
vec_mat_df_2 <- rep(2:6, each = 2)
vec_mat_df_3 <- runif(10, min = 3, max = 100)
vec_mat_df_4 <- sequence(10)

vec_mat_df_1
vec_mat_df_2
vec_mat_df_3
vec_mat_df_4
  1. Create a new matrix by combining each vector as a column, cbind().
new_matrix <- cbind(vec_mat_df_1, vec_mat_df_2, vec_mat_df_3, vec_mat_df_4)
new_matrix
str(new_matrix)
class(new_matrix)
  1. Create a new matrix by combining each vector as a row, rbind().
new_matrix <- rbind(vec_mat_df_1, vec_mat_df_2, vec_mat_df_3, vec_mat_df_4)
new_matrix
str(new_matrix)
class(new_matrix)

9.4 Exercises

  1. Create a numeric vector with values 1 through 5 using c() and extract the middle element from the vector using the described subsetting methods.
  2. Generate a sequence of even numbers from 2 to 10 using the seq() function, and calculate the total sum and average of this new vector.
  3. Create a vector of student ages (between 16 and 20, n = 10), calculate their average and standard deviation, and identify students younger than 18.
  4. Use logical indexing to subset elements from a vector based on a condition.
  5. Perform element-wise addition and multiplication on two newly created numeric vectors of the same length.
  6. Concatenate two newly created character vectors to create a longer vector.
  7. Create a named vector with elements representing days of the week.
  8. Use vector functions to find the length, sum, and mean of a numeric vector.
  9. Use the functions sort() or rev() to arrange elements in ascending, descending, or reversed order.
  10. Use functions like which() or match() to locate elements based on conditions or matching values.
  11. How can you append elements to an existing vector?
  12. When binding vectors together, what is the difference between cbind() and rbind()?
  13. Explore other vector capabilities like recycling, naming elements, and using vector lengths in your R code.

9.5 Summary

Vectors are an essential data structure for data analysis purposes. In this chapter, I have demonstrated the different types of vectors, how to generate vectors, and the various types of vector operations that can be performed. Now that we have a deeper understanding of vectors, it is time to explore other data structures (that are useful for data analysis) in the subsequent chapters.