Lecture 2: Vectors
August 4, 2026
๐ Last lecture we started by going through course logistics ๐ฅฑ before looking into:
We then looked at some basic programming concepts such as:
<-)๐ This lecture we continue our introduction, looking at
\[ \vec{x} = \begin{bmatrix} x_1 \\ \vdots \\ x_n\end{bmatrix};\qquad \mathbf{X} = \begin{bmatrix} x_{11} & \cdots & x_{1p} \\ \vdots & \ddots & \vdots \\ x_{n1} & \cdots & x_{np}\end{bmatrix}; \qquad L = \{ x,~\vec{x},~X \}. \]

There are a number of different ways of constructing vector objects in R.
c()c().
help function ?c to learn more about the c() function.โNote that all elements of a vector must be of the same data type.
rep() Functionrep() repeats a value a specified number of times.seq() Functionseq() creates a vector of consecutive values.
seq().Try to always think about the most efficient way to create a vector, as there are often multiple ways to do so!
length() Functionlength() function.typeof() Functiontypeof() function:is.type Functionsis.numeric(), is.character, and is.logical functions to check the datatype of a vector:Did anybody notice something interesting about the typeof() function output for comb_1?
Typically, numeric data is by default saved as a double unless we specify that it should be saved as an integer using the as.integer() function.
02:00
For the next 2 minutes consider the following example.
Your somewhat unhelpful team mate on a class project reports the result of their survey of 20 members of the public about their pets:
The first 10 people I asked said they had dogs, then it was back and forth between cat and dog for 8 people, and the last two were fish.
Create a vector called pets that contains the 20 values of the survey results.
rep() and c() functions: [1] "dog" "dog" "dog" "dog" "dog" "dog" "dog" "dog" "dog" "dog"
[11] "cat" "dog" "cat" "dog" "cat" "dog" "cat" "dog" "fish" "fish"
03:00
Suppose a class of 5 students have midterm scores of 73, 82, 86, 86, 94 and final exam scores 85, 71, 83, 92, 92.
midterm_scores and final_scores storing the midterm and final score values.final_grade consisting of the studentsโ final grades which are calculated as follows:\[ \text{final} = \frac{(2\times \text{midterm}) + (3\times\text{final})}{5}. \]
c() to define the vectors:R we use braces vec_1[...]. [1] 1 2 3 4 1 2 3 4 1 2 3 4
[1] TRUE TRUE FALSE FALSE FALSE
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00 1.70 1.70
comb_2 because the logical vector is TRUE for those indices.โThis will become very important when we look at filtering later in this lecture.
For convenience we can assign names to each vector index.
The names() function can be used to both inspect the current names of vector indices, and assign new names to vector indices.
Suppose the students from our exercise on grades a few slides ago (in the correct order) are named: Alex, Becca, Ciar, Dobby, and Eliah.
We can inspect the current index names using the names() function (currently empty).
names() function with the assignment operator:[1] "Alex" "Becca" "Ciar" "Dobby" "Eliah"
03:00
For the next 3 minutes complete the following exercise.
The following 8 values are an individualโs monthly grocery bill from January through August:
326, 281, 287, 308, 405, 299, 421, 301
grocery with the 8 values. Check that all 8 are accounted for.Jan, Feb, Mar, Apr, May, Jun, Jul, Aug.Jan, Mar and Aug combined.๐ The computation still runs, but this is a common source of silent bugs โ always double-check your vector lengths before combining them.
%% tells you if one length divides evenly into the other โ a non-zero result means expect a warning.[1] "cat" "dog" "2" "monkey" "7" "9"
2, 7 and 9 were converted:Logical & Numeric Values
Logical, Numeric & Character Strings
๐ We notice an order of priority: (1) Character strings; (2) Numeric; and (3) Boolean.
๐ Because nums was coerced to character, R can no longer do arithmetic on it โ always check typeof() if a calculation errors unexpectedly.
as.numeric(), as.character(), or as.logical() to explicitly convert a vector back to the type you need.sort() Functionsort() function.decreasing = TRUE argument:FALSE is always treated as smaller than TRUE:order() Functionsort() only reorders the values of a single vector โ but what if we want to reorder a second, related vector to match?order() function returns the indices that would sort a vector, rather than the sorted values themselves:๐ This is the standard way to keep multiple related vectors in sync when sorting by one of them.
05:00
Spend the last 5 minutes completing the following exercises, combining all of the skills you have developed throughout the lecture.
num_seq as a vector of consecutive integers starting at 1 and ending at 500.rev_seq which consists of all numbers between 1 and 5 and between 495 and 500, displayed in descending order.