Skip to main content

Section 1.5 Vectors

Now that objects have been created, it is time to use those same principles to create vectors. Like objects, vectors contain information, just more of it. Before, we had x <- 5, however, what if we wanted x to contain every single letter in the alphabet, and not just one thing (5)?
This is exactly where vectors come in.

There are two very important things that come into play when creating a vector.

  1. Everything must go inside parenthesis
  2. Before the parenthesis, you have to call the c function. The purpose of this is to literally combine values into a vector.
You’re telling R β€œPlease combine everything in the parenthesis into one vector.”
For instance, if we wanted to create a vector that has a, b, and c in it, the below code wouldn’t work.
wont_work <- a, b, c # no parenthesis

also_wont_work <- (a, b, c) # no c before the parenthesis
Since we have vector examples that don’t work, let’s get into examples that do work.

Subsection 1.5.1 Numeric Vectors

Numeric vectors contain only numeric values inside of them.
# Numeric vector
number_vector <- c(1, 2, 3, 4, 5) # c stands for combine/concatenate

number_vector

numeric_vector_2 <- c(6:10) # The colon is like saying "everything from 6 to 10"

numeric_vector_2

# The length command tells you how many values are inside the vector
length(number_vector) # 5

length(numeric_vector_2) # 5

# It is possible to add numeric vectors as long as they're the same length

number_vector + numeric_vector_2

# Vectorized math
number_vector * 10
[1] 1 2 3 4 5
[1]  6  7  8  9 10
[1] 5
[1] 5
[1]  7  9 11 13 15
[1] 10 20 30 40 50

Subsection 1.5.2 Character Vectors

Just like with numeric data, we can create vectors with characters inside. Unlike with numeric values in R, characters must be in between quotation marks. Notice once quotation marks are used, everything inside (and the quotation marks themselves) change color in your script.

Quotation Marks.

Be careful, as the positioning of quotation marks can severely change your vector (see below).
# Quotation marks around each of the fruits
fruits <- c("apple", "banana", "cherry")

fruits

# Quotation marks around the entire string
Fruits <- c("apple, banana, cherry")

Fruits
[1] "apple"  "banana" "cherry"
[1] "apple, banana, cherry"
Take note how in fruits each fruit was it’s own value, but in Fruits, they were together.

Subsection 1.5.3 Logical Vectors

Time for a quick question:.

True or False: logical vectors are possible in R?
True!
R is able to handle logistic values (True and False). In R, you can either write it as the full word in all caps or the first letter capitalized:

See anything in particular regarding your code?

Notice how in your code, logic also changes color, the same color as numeric.
# Using full word capitalized
some_truths <- c(TRUE, FALSE, TRUE)

some_truths

class(some_truths)

# Using first letter capitalized
some_lies <- c(F, F, T)

some_lies

class(some_lies)

# Once you put quotation marks, it makes them a character
truth_logic <- c("TRUE","FALSE","TRUE")

class(truth_logic)
[1]  TRUE FALSE  TRUE
[1] "logical"
[1] FALSE FALSE  TRUE
[1] "logical"
[1] "character"
Above we used the class() command to check to see what type of data we were creating. some_truth and some_lies were both logical, while truth_logic was character.

When creating something in R:.

It is always crucial to check that what you’ve created matches your desired output.

Subsection 1.5.4 Factors (categorical)

When character values have levels, it becomes categorical data. In R, this is best handled as factors. To turn a character string into factors, utilize the factor() command.

Look at your screen!

Notice how when you type the name of a color in R, the text actually changes to that color. R really does put the r in Artist!
# Creating the colors vector as factors
colors <- factor(c("red", "blue", "red", "green")) # 4 values

colors

class(colors)

levels(colors) # Checking to see how many levels there are (3)
[1] red   blue  red   green
Levels: blue green red
[1] "factor"
[1] "blue"  "green" "red"
When we called class() we saw that colors was factor, not character. In order to check to see how many levels were inside colors, we used the levels() command, which indicated that there were three: blue, green, and red.

Subsection 1.5.5 Indexing (1-based in R!)

Vectors, unlike objects, have multiple values inside of them. For instance, when we created the vector fruits, it had three values: apple, banana, cherry. Vectors inherently are indexed meaning that there is positioning inside of the vector. For fruits, apple would be 1, banana would be 2, and cherry would be 3. In R, we can utilize this indexing to our advantage and call specific positions from vectors. To do this, we will be using brackets.
# fruits <- c("apple", "banana", "cherry")
fruits[1] # first element

fruits[2:3] # slice
[1] "apple"
[1] "banana" "cherry"
No matter how many values are in a vector, you can always use [] to call specific values.

Subsection 1.5.6 Type Coercion

In the previous sections, we created vectors, but they’ve all been the same data type. What about if we put some different types of data, for instance, numeric and character, into a vector?
# Putting two numeric and one character into a vector.
mix <- c(1, 2, "three")

mix # becomes character

class(mix)
[1] "1"     "2"     "three"
[1] "character"
It defaults to character!

As a rule of thumb:.

Quotation marks rule. If a vector has quotation marks, R defaults it to a character string.
With ample discussion around vectors, it is time to build even more: data frames.