A vector is a container of (multiple) elements:

Some examples of a vector (and its type):

Vector is the primary data structure of the R language.

Numerical vector

A vector of numbers you can create with the combine function c.
Type the following to create a vector of numbers and store it in variable x:

x <- c(3.5, 6, 7, 1+1)
x
[1] 3.5 6.0 7.0 2.0

Calculations or functions often work on vectors elementwise.
This is helpful to do multiple calculations simultaneously:

x - 18
[1] -14.5 -12.0 -11.0 -16.0
x^2
[1] 12.25 36.00 49.00  4.00
sqrt(x)
[1] 1.870829 2.449490 2.645751 1.414214

Some functions summarize a vector to a single number:

sum(x)
[1] 18.5
mean(x)
[1] 4.625

Sequence of numbers

A simple regular sequence you can create with : (colon operator). Try:

y <- 1:10
y
 [1]  1  2  3  4  5  6  7  8  9 10
7:9
[1] 7 8 9

You can also use the function seq:

x <- seq(5,15,3)
x
[1]  5  8 11 14

Character vector

A character vector, so a vector of any texts, you may also create with the combine function c:

x <- c( "Jay", "Gloria", "Claire", "Phil", 'Mitchell', 'Cameron' )
x
[1] "Jay"      "Gloria"   "Claire"   "Phil"     "Mitchell" "Cameron" 

Both single quotes ('word') and double quotes ("word") are allowed (must be identical at the beginning and the end of a text).

Logical vector

In R the following words denote logical values: FALSE and TRUE.

Typically, logical vectors are results of logical conditions. Try yourself:

x <- c( 0, 1.5, 5 )
x > 2           # The result of greater-sign-operator is a logical vector
[1] FALSE FALSE  TRUE

The function sum might be used on a logical vector to count how many elements are TRUE. Enter:

sum( x > 2 )
[1] 1

We can also explicitely store a calculated logical vector to a variable:

v <- ( x > 2 )
v
[1] FALSE FALSE  TRUE

Of course, a vector of logical values can also be created manually with the combine function c:

x <- c( TRUE, TRUE, FALSE, TRUE, FALSE )
x
[1]  TRUE  TRUE FALSE  TRUE FALSE

Missing values

Use the word NA (not available) to represent a single missing value.
Missing values can appear in vectors of any type (see examples in the next section).

When a missing value appears in a calculation, the result is usually also a missing value:

x <- c( 1, 2, NA, 4, 5 )
x + 3
[1]  4  5 NA  7  8

The function is.na produces a logical vector which is TRUE at the positions at which the missing values are. Try:

x
[1]  1  2 NA  4  5
is.na( x )
[1] FALSE FALSE  TRUE FALSE FALSE

When combined with the function sum, it allows to count the number of missing values in a vector. Try:

sum( is.na( x ) )
[1] 1

Or, when negated (with ! symbol) it allows to count the number of not missing values in a vector. Try:

sum( !is.na( x ) )
[1] 4

Length and elements of a vector

Use length to find out the length (number of elements) in a vector (numerical, character, logical, …):

x <- c( 11, 12, NA, 14 )
length(x)
[1] 4
y <- c( "Jay", "Gloria", "Claire", NA, 'Mitchell', 'Cameron' )
length(y)
[1] 6
z <- c( FALSE, TRUE, NA, FALSE )
length(z)
[1] 4

Use square brackets to select an element from a vector.
The first element is at position 1. Try the following to select an element from the second position:

x[2]
[1] 12
y[2]
[1] "Gloria"
z[2]
[1] TRUE

Now try to combine the brackets with length to select the last element. Type:

x[ length(x) ]
[1] 14
y[ length( y ) ]
[1] "Cameron"
z[length(z)]
[1] FALSE

Try the following to select multiple elements at once:

x[ c(1,3) ]
[1] 11 NA
w <- 2:4
x[w]
[1] 12 NA 14
z[c(1,1,1,1,2)]
[1] FALSE FALSE FALSE FALSE  TRUE

Note, when a vector is printed, the number reported on the left in square brackets shows the position of the next vector element:

x <- 101:155
x
 [1] 101 102 103 104 105 106 107 108 109 110 111 112
[13] 113 114 115 116 117 118 119 120 121 122 123 124
[25] 125 126 127 128 129 130 131 132 133 134 135 136
[37] 137 138 139 140 141 142 143 144 145 146 147 148
[49] 149 150 151 152 153 154 155

Type (class) of a vector

Sometimes you may need to find what type of a vector is stored in a variable.
This can be done with the function class.

Type the following to create vectors x, y and z:

x <- c( 11, 12, NA, 14 )
y <- c( "Jay", "Gloria", "Claire", NA, 'Mitchell', 'Cameron' )
z <- c( FALSE, TRUE, NA, FALSE )

To find the types of these vectors enter:

class( x )
[1] "numeric"
class(y)
[1] "character"
class( z )
[1] "logical"


Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC