Other sites

Combine values into a vector

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Combine values into a vector

Use the concatenate function c() to create vectors of different data types

1:100
c(1, 2, 3, 4)
c("abc", "def", "ghi")
c(TRUE, FALSE, TRUE)

Introduction to Vectors

A vector is a collection of elements of the same kind and the most basic data structure in R. For example, a vector could hold the four numbers 1, 3, 2 and 5. Another vector could be formed with the three text strings "Welcome", "Hi" and "Hello". These different kinds of values (numbers, text) are called data types.

A single value is also treated as a vector – a vector with only one element in it. As we will see throughout the course, this concept makes R very special. We can manipulate vectors and its values through plenty of operations that are provided by R.

One key advantage of vectors is that we can apply an operation (e.g. a multiplication) to all its values at once instead of going through each item individually. This is called vectorization.

Types of vectors

Vectors can only hold elements of the same data type. In this course we will work with the following three main data types:

Numeric values are numbers. Although they can be further split into whole numbers (integers) and numbers with decimals (doubles), R automatically converts between these sub-types if needed. Therefore, we will collectively refer to them as just numeric values.

Character values contain textual content. These can be letters, symbols, spaces and numbers as well. They must be enclosed by quotation marks – either single quotes '___' or double quotes "___".

Logical values can either be TRUE or FALSE. They are also often referred to as boolean or binary values. Because a logical value can only be TRUE or FALSE they are most often used to answer simple questions like “Is 1 greater than 2?” or “Is it past 3 o’clock?”. These kind of questions only need answers like “Yes” (TRUE) or “No” (FALSE). Importantly, in R logical values are case sensitive, which means they have to be written with capital letters.

Quiz: Data Types

Which of the following options are valid data types in R?Start Exercise

Creating a sequence of numbers

1:100
c(1, 2, 3, 4)
c("abc", "def", "ghi")
c(TRUE, FALSE, TRUE)

In R, even a single value is considered a vector. Creating a vector of one element is as simple as typing its value:

4

[1] 4

To create a sequence of numeric values we can use the : operator, which takes two numbers and outputs a vector of all whole numbers in that range:

2:11

[1] 2 3 4 5 6 7 8 9 10 11

The : operator creates a vector from the number on the left-hand side to the number on the right-hand side. Therefore, the order of numbers is important. If we define the previous example the other way around, we get a vector of descending numbers, instead of ascending:

11:2

[1] 11 10 9 8 7 6 5 4 3 2

The : operator comes handy when we need a vector of every whole number in a given range. However, if we need a vector where the numbers aren’t linear, we require something different.

Exercise: Use the : operator

Concatenating numeric values to a vector

1:100
c(1, 2, 3, 4)
c("abc", "def", "ghi")
c(TRUE, FALSE, TRUE)

We can combine multiple numbers into a single vector using the concatenate function c() which links elements between the round braces together into a chain. Multiple elements need to be separated by commas.

To create our first vector holding seven different numbers we can use the concatenate function c() like so:

c(7, 4, 2, 5, 5, 22, 1)

[1] 7 4 2 5 5 22 1

Note, that the “[1]” sign before the output above is added by R, and is always added automatically when printing out vectors. If your vectors become bigger you will see more of these prefixes. Just know that they are only added for informational purposes by R, and that they are there to help you while coding. They are not part of the vector itself.

You can see this more clearly, when the output spans over multiple lines:

Exercise: Concatenate text

Creating logical vectors

1:100
c(1, 2, 3, 4)
c("abc", "def", "ghi")
c(TRUE, FALSE, TRUE)

Logical vectors can only hold the values TRUE and FALSE. To create a logical vector with a single value, type out one of the valid values TRUE or FALSE. Remember that they must be written with capital letters:

TRUE

[1] TRUE

Similarly to other types of vectors, we can use the concatenate function c() to create a logical vector of multiple elements:

c(TRUE, FALSE, TRUE, FALSE, TRUE)

[1] TRUE FALSE TRUE FALSE TRUE

Exercise: Concatenate logical values

Use the concatenate function c() and create a vector containing the three elements: TRUE, FALSE and TRUE