Combine values into a vector

May 1, 2020
By

[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

  • Everything is a vector
  • Get to know different data types in R
  • Learn how to create vectors
  • Use the : operator to create numeric sequences
  • 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

Use the : operator and create a vector from 2 to 6

Start Exercise

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:

1:60
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
[22] 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
[43] 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

Exercise: Concatenate numbers

Use the concatenate function c() and create a vector containing the numbers 2, 3, 6 and 7

Start Exercise

Creating character vectors

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

To create a character vector of one element, all we need to do is to type out the text. Remember that we need to use quotation marks (" ") around character values:

"golden retriever"
[1] "golden retriever"

To create a character vector of multiple elements, we can again use the concatenate function c(). This time we will use it with characters instead of numbers:

c("golden retriever", "labrador is a family dog", "beagle")
[1] "golden retriever"         "labrador is a family dog"
[3] "beagle"                  

Exercise: Create a character vector

Create a character vector with the single element: "R is awesome!"

Start Exercise

Exercise: Concatenate text

Use the concatenate function c() and create a vector containing four elements:

  1. "wombat",
  2. "fennec fox",
  3. "bearded dragon" and
  4. "tasmanian devil"

Start Exercise

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

Start Exercise

Quiz: Vectors Recap

Which of the following statements about vectors are correct?
Start Exercise

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)