my_vec <- c(2, 3, 1, 6, 4, 3, 3, 7)
my_vec
#> [1] 2 3 1 6 4 3 3 72 Vectors
1 Creating vectors in R
Up until now we’ve been creating simple objects by directly assigning a single value to an object.
It’s very likely that you’ll soon want to progress to creating more complicated objects. Happily, R has a multitude of functions to help you do this
The first function we will learn about is the
c()function.The
c()function is short for concatenate and we use it to join together a series of values and store them in a data structure called a vector or “atomic vector”
Now that we’ve created a vector. we can use other functions to do useful stuff with this object
For example, we can calculate the mean, variance, standard deviation and number of elements in our vector by using the
mean(),var(),sd()andlength()functions
Scalar is a vector of length one
- If we wanted to use any of these values later on in our analysis we can just assign the resulting value to another object.
vec_mean <- mean(my_vec) # returns the mean of my_vec
vec_mean
#> [1] 3.6252 Vector basics
- Atomic vectors are of six types:
logicalintegerdoublecharactercomplexraw
- Integer and double vectors are collectively known as numeric vectors.

-
Every vector has two key properties:
- Its type, which you can determine with
typeof()
- Its type, which you can determine with
- Its length, which you can determine with
length().
length(my_vec)
#> [1] 8- Vectors can also contain arbitrary additional metadata in the form of attributes (More on this later)
3 Logical vectors
Logical vectors are the simplest type of atomic vector because they can take only two possible values: FALSE and TRUE
x_l <- c(TRUE, FALSE, TRUE)
x_l
#> [1] TRUE FALSE TRUE
typeof(x_l)
#> [1] "logical"
is.logical(x_l)
#> [1] TRUE| logical operator | symbol in R |
|---|---|
| equal to | == |
| greater or greater equal |
>,>=
|
| less or less equal |
<,<=
|
| not equal | != |
10 == 15
#> [1] FALSE
10 != 15
#> [1] TRUE
10 > 15
#> [1] FALSE
10 < 15
#> [1] TRUE4 Numeric vectors
Integer and double vectors are known collectively as numeric vectors
In R, numbers are doubles by default. To make an integer, place an
Lafter the number:
(x_i <- c(1L, 50L, 20L, 32L))
#> [1] 1 50 20 32
typeof(x_i); is.integer(x_i)
#> [1] "integer"
#> [1] TRUE5 Character vectors
Character vectors are used to represent string values. You can think of character strings as something like a word (or multiple words).
It is represented by a collection of characters between double quotes (
")
x_c <- c("boy", "boy", "girl")
x_c
#> [1] "boy" "boy" "girl"
typeof(x_c)
#> [1] "character"
is.character(x_c)
#> [1] TRUE6 Missing values
-
NULLis often used to represent the absence of a vector-
NULLtypically behaves like a vector of length 0
-
my_vec1 <- NULL
my_vec1 <- c(my_vec1, 10)
my_vec1
#> [1] 10-
NAis used to represent the absence of a value in a vector.
my_vec2 <- c(18, 21, NA, 22)
my_vec2
#> [1] 18 21 NA 22intergeranddouble\(\rightarrow\) quantitative datacharacter\(\rightarrow\) qualitative datalogical\(\rightarrow\) binary data
7 Sequence of numbers
Sometimes it can be useful to create a vector that contains a regular sequence of values in steps of one.
Here we can make use of a shortcut using the
:(colon) symbol.
my_seq <- 1:10 # create regular sequence
my_seq
#> [1] 1 2 3 4 5 6 7 8 9 10
my_seq2 <- 10:1 # in decending order
my_seq2
#> [1] 10 9 8 7 6 5 4 3 2 1
-5:4
#> [1] -5 -4 -3 -2 -1 0 1 2 3 4
8 Sequence of numbers: seq()
Other useful functions for generating vectors of sequences include the
seq()andrep()functions.For example, to generate a sequence from
1to5in steps of0.5
Here we’ve used the arguments
from =andto = todefine the limits of the sequence and theby =argument to specify the increment of the sequence.Play around with other values for these arguments to see their effect
9 Repeat vectors using rep()
- The
rep()function allows you to replicate (repeat) values a specified number of times. To repeat the value 2, 10 times
rep(2, times = 10)
#> [1] 2 2 2 2 2 2 2 2 2 2The arguments
times,eachandlength.outare used inrep()to obtain different vectorsWe can also repeat non-numeric values. e.g.
rep("boy", times = 3)
#> [1] "boy" "boy" "boy"10 Exercise 2
- Create the vector (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) in three ways:
- Create the vector (2.1, 4.1, 6.1, 8.1) in two ways:
- Create the vector (0, 5, 10, 15) in 3 ways:
- (1, 1, 2, 2, 3, 3, 4, 4)
- (1, 2, 2, 3, 3, 3, 4, 4, 4, 4)
- (-0.50, -0.25, 0, 0.25, 0.5, 0.75, 1)
Create the vector (101, 102, 103, 200, 205, 210, 1000, 1100, 1200) using a combination of the
c()andseq()functionsCreate a vector that repeats the integers from 1 to 5, 10 times, i.e. (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, \(\ldots\)), and the length of the vector should be 50!
Create the same vector as before, but this time repeat 1, 10 times, then 2, 10 times, etc., i.e. (1, 1, 1, \(\ldots\), 2, 2, 2, \(\ldots\), \(\ldots\), 5, 5, 5) and the length of the vector should also be 50