Introduction to R and RStudio

Chapter 6 Vectors

The simplest and most common data structure in R is the vector.

Vectors come in two different flavors: atomic vectors and lists. An atomic vector contains exactly one data type, whereas a list may contain multiple data types. We’ll explore atomic vectors further before we get to lists.

In previous lessons, we dealt entirely with numeric vectors, which are one type of atomic vector. Other types of atomic vectors include logical, character, integer, and complex. In this lesson, we’ll take a closer look at logical and character vectors.

Logical vectors can contain the values TRUE, FALSE, and NA (for ‘not available’). These values are generated as the result of logical ‘conditions’. Let’s experiment with some simple conditions.

First, create a numeric vector num_vect that contains the values 0.5, 55, -10, and 6.

num_vect <- c(0.5, 55, -10, 6)

Now, create a variable called tf that gets the result of num_vect < 1, which is read as ‘num_vect is less than 1’.

tf <- num_vect < 1

What do you think tf will look like?

  1. a vector of 4 logical values
  2. a single logical value

a vector of 4 logical values

Print the contents of tf now.

tf
## [1]  TRUE FALSE  TRUE FALSE

The statement num_vect < 1 is a condition and tf tells us whether each corresponding element of our numeric vector num_vect satisfies this condition.

The first element of num_vect is 0.5, which is less than 1 and therefore the statement 0.5 < 1 is TRUE. The second element of num_vect is 55, which is greater than 1, so the statement 55 < 1 is FALSE. The same logic applies for the third and fourth elements.

Let’s try another. Type num_vect >= 6 without assigning the result to a new variable.

num_vect >= 6
## [1] FALSE  TRUE FALSE  TRUE

This time, we are asking whether each individual element of num_vect is greater than OR equal to 6. Since only 55 and 6 are greater than or equal to 6, the second and fourth elements of the result are TRUE and the first and third elements are FALSE.

The < and >= symbols in these examples are called ‘logical operators’. Other logical operators include >, <=, == for exact equality, and != for inequality.

If we have two logical expressions, A and B, we can ask whether at least one is TRUE with A | B (logical ‘or’ a.k.a. ‘union’) or whether they are both TRUE with A & B (logical ‘and’ a.k.a. ‘intersection’). Lastly, !A is the negation of A and is TRUE when A is FALSE and vice versa.

It’s a good idea to spend some time playing around with various combinations of these logical operators until you get comfortable with their use. We’ll do a few examples here to get you started.

Try your best to predict the result of each of the following statements. You can use pencil and paper to work them out if it’s helpful. If you get stuck, just guess and you’ve got a 50% chance of getting the right answer!

(3 > 5) & (4 == 4)

  1. TRUE
  2. FALSE

FALSE

(TRUE == TRUE) | (TRUE == FALSE)

  1. TRUE
  2. FALSE

TRUE

Character vectors are also very common in R. Double quotes are used to distinguish character objects, as in the following example.

Create a character vector that contains the following words: “My”, “name”, “is”. Remember to enclose each word in its own set of double quotes, so that R knows they are character strings. Store the vector in a variable called my_char.

my_char <- c("My", "name", "is")

Print the contents of my_char to see what it looks like.

my_char
## [1] "My"   "name" "is"

Right now, my_char is a character vector of length 3. Let’s say we want to join the elements of my_char together into one continuous character string (i.e. a character vector of length 1). We can do this using the paste() function.

Type paste(my_char, collapse = ” “) now. Make sure there’s a space between the double quotes in the collapse argument. You’ll see why in a second.

paste(my_char, collapse = " ")
## [1] "My name is"

The collapse argument to the paste() function tells R that when we join together the elements of the my_char character vector, we’d like to separate them with single spaces.

It seems that we’re missing something…. Ah, yes! Your name!

To add (or ‘concatenate’) your name to the end of my_char, use the c() function like this: c(my_char, “your_name_here”). Place your name in double quotes where I’ve put “your_name_here”. Try it now, storing the result in a new variable called my_name.

my_name <- c(my_char, "Swirl")

Take a look at the contents of my_name.

my_name
## [1] "My"    "name"  "is"    "Swirl"

Now, use the paste() function once more to join the words in my_name together into a single character string. Don’t forget to say collapse = ” “!

paste(my_name, collapse = " ")
## [1] "My name is Swirl"

You’ve successfully completed this lesson!