The Basics of R and RStudio

Part 5: Vectors

RStudio
R
Tutorial
Blog
Author

William Okech

Published

November 12, 2022

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 Part 7). 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 are shown below:

• Numbers: c(2, 10, 16, -5)

• Characters: c("R", "RStudio", "Shiny", "Quarto")

• Logicals: c("TRUE", "FALSE", "TRUE")

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
 [1]  9 10 11 12 13 14 15 16 17 18
a_rev <- 18:9
a_rev
 [1] 18 17 16 15 14 13 12 11 10  9
a_rev_minus <- 5:-3
a_rev_minus
[1]  5  4  3  2  1  0 -1 -2 -3

Generate a sequence using sequence()

b <- sequence(7)
b
[1] 1 2 3 4 5 6 7
c <- sequence(c(5,9))
c
 [1] 1 2 3 4 5 1 2 3 4 5 6 7 8 9

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
 [1]  2  4  6  8 10 12 14 16 18 20
f <- seq(2,20, length.out=5)
f
[1]  2.0  6.5 11.0 15.5 20.0
h <- seq(20,2,by=-2)
h
 [1] 20 18 16 14 12 10  8  6  4  2
j <- seq(20, 2, length.out=3)
j
[1] 20 11  2

Repeating vectors

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

k <- rep(c(0,3,6), times = 3)
k
[1] 0 3 6 0 3 6 0 3 6
l <- rep(2:6, each = 3)
l
 [1] 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6
m <- rep(7:10, length.out = 20)
m
 [1]  7  8  9 10  7  8  9 10  7  8  9 10  7  8  9 10  7  8  9 10

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.

Basic Vector Operations

vec_1 <- 1:10

vec_1*12 # multiplication
 [1]  12  24  36  48  60  72  84  96 108 120
vec_1+12 # addition
 [1] 13 14 15 16 17 18 19 20 21 22
vec_1-12 # subtraction
 [1] -11 -10  -9  -8  -7  -6  -5  -4  -3  -2
vec_1/3 # division
 [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667 2.0000000 2.3333333
 [8] 2.6666667 3.0000000 3.3333333
vec_1^4 # power
 [1]     1    16    81   256   625  1296  2401  4096  6561 10000
sqrt(vec_1) # square root
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
 [9] 3.000000 3.162278

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
 [1]  5  6  7  8  9 10 11 12 13 14
vec_4 <- 12:3
vec_4
 [1] 12 11 10  9  8  7  6  5  4  3
  1. Perform various arithmetic operations
vec_3 + vec_4
 [1] 17 17 17 17 17 17 17 17 17 17
vec_3 - vec_4
 [1] -7 -5 -3 -1  1  3  5  7  9 11
vec_3 / vec_4
 [1] 0.4166667 0.5454545 0.7000000 0.8888889 1.1250000 1.4285714 1.8333333
 [8] 2.4000000 3.2500000 4.6666667
vec_3 * vec_4
 [1] 60 66 70 72 72 70 66 60 52 42
vec_3 ^ vec_4
 [1] 244140625 362797056 282475249 134217728  43046721  10000000   1771561
 [8]    248832     28561      2744

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)
[1] TRUE
any(vec_3 < vec_4)
[1] TRUE
all(vec_3 > vec_4)
[1] FALSE
all(vec_3 < vec_4)
[1] FALSE
length(vec_3)
[1] 10
length(vec_4)
[1] 10
typeof(vec_3)
[1] "integer"
typeof(vec_4)
[1] "integer"

Determine the number of letters in a character

vec_5 <- c("R", "RStudio", "Shiny", "Quarto")
nchar(vec_5)
[1] 1 7 5 6

Recycling of vectors

vec_3 + c(10, 20)
 [1] 15 26 17 28 19 30 21 32 23 34
vec_3 + c(10, 20, 30) # will result in a warning as the longer vector is not a multiple of the shorter one
Warning in vec_3 + c(10, 20, 30): longer object length is not a multiple of
shorter object length
 [1] 15 26 37 18 29 40 21 32 43 24

Accessing elements of a vector

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().

Create the vector.

vec_name <- 1:5
vec_name
[1] 1 2 3 4 5

Name the individual elements.

names(vec_name) <- c("a", "c", "e", "g", "i")
vec_name
a c e g i 
1 2 3 4 5 

2. Use the vector index to filter

vec_index <- 1:5
vec_index
[1] 1 2 3 4 5
a) Logical vector as an index
vec_index[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
[1] 1 3 5
b) Filter vector based on an index
vec_index[1:3]
[1] 1 2 3
c) Access a vector using its position
vec_index[4]
[1] 4
vec_index[c(2,4)]
[1] 2 4
d) Modify a vector using indexing
vec_index
[1] 1 2 3 4 5
vec_index[5] <- 1000
vec_index
[1]    1    2    3    4 1000