# 2 - Vectors ------------------------------------------------------------------
# 2.1 - Single-element vectors (and quoting)
# 2.2 - Multi-element vectors
# 2.3 - Vectorization
# Challenge 1
# - Make a vector `x` with the whole numbers 1 through 26.
# Then, subtract 0.5 from each element in `x` and save the result in vector `y`.
# Check your results by printing both vectors.
# - Bonus: What do you think the result of the following operation will be?
# 1:5 * 1:5
# We didn't cover this kind of scenario, but go ahead and test your intuition!
# After you've decided on your expectation,
# run the code and check if you were correct.
# 2.4 - Exploring vectors
# 2.5 - Extracting element from vectors
# 3 - Data frames --------------------------------------------------------------
# 4 - Data types ---------------------------------------------------------------
# 4.1 - R's main data types
# 4.3 - A vector can only contain one data type
# Challenge 2
# What type of vector (if any) do you think each of the following will produce?
# Try it out and see if you were right.
# typeof("TRUE")
# typeof(banana)
# typeof(c(2, 6, "3"))
# 4.5 - Manual type conversionR’s data structures and data types
1 Introduction
1.1 What we’ll cover
In this session, you will learn about some of the nuts and bolts of R, which will give you a solid foundation to start doing more exciting things in the next two sessions of this workshop: data wrangling and data visualization.
Specifically, we will cover R’s data structures and data types.
Data structures are the kinds of objects that R can store data in. Here, we’ll see the two most common ones: vectors and data frames.
Data types are how R distinguishes between different kinds of data like numbers versus character strings. Here, we’ll talk about the 4 main data types:
character,integer,double, andlogical.
An analogy: If this was about food rather than data, you can think of different data types as different food items, and various data structures as different types of containers to store food in.
1.2 Setting up
To make it easier to keep track of what we do, we’ll write our code in a script:
Open a new R script: Click the
+symbol in the top toolbar, then clickR Script1.Save the script straight away as
data-structures.R. You can save it anywhere you like, but ideally in a folder that is specifically for this workshop.If you want section headers as comments in your script, like in the script I am showing you in the live session, then copy-and-paste the following into your script:
Section headers for your script (Click to expand)
2 The vector data structure
The first data structure we will explore is the simplest: the vector. A vector in R is essentially a collection of one or more items. Moving forward, we’ll call such individual items “elements”.
2.1 Single-element vectors (and quoting)
Vectors can consist of just a single element, so each of the two lines of code below creates a vector:
vector1 <- 8
vector2 <- "panda"In the "panda" example, which is a character string (string for short):
"panda"constitutes one element, not 5 (its number of letters).- Unlike when dealing with numbers, we have to quote the string.2
Character strings need to be quoted because they are otherwise interpreted as R objects – for example, because our vectors vector1 and vector2 are objects, we refer to them without quotes:
# [Note that R will show auto-complete options after you type 3 characters]
vector1[1] 8
vector2[1] "panda"
Meanwhile, the code below doesn’t work because there is no object called panda:
vector_fail <- pandaError: object 'panda' not found
2.2 Multi-element vectors
A common way to make vectors with multiple elements is by using the c (combine) function:
c(2, 6, 3)[1] 2 6 3
c() can also append elements to an existing vector:
# First we create a vector:
vector_to_append <- c("cardinal", "chickadee")
vector_to_append[1] "cardinal" "chickadee"
# Then we append another element to it:
c(vector_to_append, "bald eagle")[1] "cardinal" "chickadee" "bald eagle"
To create vectors with series of numbers, a couple of shortcuts are available. First, you can make series of whole numbers (integers) with the : operator:
1:10 [1] 1 2 3 4 5 6 7 8 9 10
Second, you can use a function like seq() and its arguments from (starting value), to (end value), and by (step size) for fine control over the sequence:
stepwise_vec <- seq(from = 6, to = 8, by = 0.2)
stepwise_vec [1] 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0
2.3 Vectorization
Consider the output of this command:
stepwise_vec * 2 [1] 12.0 12.4 12.8 13.2 13.6 14.0 14.4 14.8 15.2 15.6 16.0
Above, every individual element in stepwise_vec was multiplied by 2. We call this behavior “vectorization” and this is a key feature of the R language. (Alternatively, you may have expected this code to repeat stepwise_vec twice, but this did not happen!)
Challenge 1
Make a vector x with the whole numbers 1 through 26. Then, subtract 0.5 from each element in x and save the result in vector y. Check your results by printing both vectors.
Click for the solution
x <- 1:26
x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26
y <- x - 0.5
y [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5 13.5 14.5
[16] 15.5 16.5 17.5 18.5 19.5 20.5 21.5 22.5 23.5 24.5 25.5
Bonus: What do you think the result of the following operation will be? We didn’t cover this kind of scenario, but go ahead and test your intuition! After you’ve decided on your expectation, run the code and check if you were correct.
1:5 * 1:5Click for the solution
1:5 * 1:5[1] 1 4 9 16 25
Both vectors are of length 5 which will lead to “element-wise matching”: the first element in the first vector will be multiplied with the first element in the second vector, the second element in the first vector will be multiplied with the second element in the second vector, and so on.
2.4 Exploring vectors
R has many functions that provide information about vectors and other types of objects, such as:
Get the number of elements with
length():length(stepwise_vec)[1] 11See the first and last few elements, respectively, with
head()andtail():# Print the first 6 elements: head(stepwise_vec)[1] 6.0 6.2 6.4 6.6 6.8 7.0# Print the last 6 elements: tail(stepwise_vec)[1] 7.0 7.2 7.4 7.6 7.8 8.0# Both head and tail have argument `n` to specify the number of elements: tail(stepwise_vec, n = 2)[1] 7.8 8.0Get arithmetic summaries like
mean()for vectors with numbers:# mean() will compute the mean (average) across all elements mean(stepwise_vec)[1] 7
2.5 Extracting elements from vectors
Extracting element from objects like vectors is often called “indexing”. In R, we can do this using “bracket notation” with square brackets [ ] – for example:
Get the second element with
[2]3:stepwise_vec[2][1] 6.2Get the second through the fifth elements with
[2:5]:stepwise_vec[2:5][1] 6.2 6.4 6.6 6.8Get the first and eight elements with
[c(1, 8)]:stepwise_vec[c(1, 8)][1] 6.0 7.4
3 The data frame data structure
One of R’s most powerful features is its built-in ability to deal with tabular data – i.e., data with rows and columns like you are familiar with from Excel spreadsheets and so on. In R, tabular data is stored in a data structure called “data frame”.
Let’s start by using the data.frame() function to make a data frame with information about 3 cats:
cats <- data.frame(
name = c("Luna", "Thomas", "Daisy"),
coat = c("calico", "black", "tabby"),
weight = c(2.1, 5.0, 3.2)
)cats name coat weight
1 Luna calico 2.1
2 Thomas black 5.0
3 Daisy tabby 3.2
Above:
- We created 3 vectors and pasted them side-by-side to create a data frame in which each vector constitutes a column.
- We gave each vector a name (e.g.,
coat), and those names became the column names. - The resulting data frame has 3 rows (one for each cat) and 3 columns (each with a type of info about the cats, like coat color).
It is good practice to organize tabular data in the so-called “tidy” data format like above, where:
- Each column contains a different “variable” (e.g. coat color, weight)
- Each row contains a different “observation” (data on e.g. one cat/person/sample)
That’s all we’ll say about data frames for now, but in today’s remaining sessions we will explore this key R data structure more!
4 Data types
4.1 R’s main data types
R distinguishes between different kinds of data, such as character strings and numbers, using several pre-defined “data types”. R’s behavior in various operations depends heavily on the data type – for example, the below fails:
"valerion" * 5Error in "valerion" * 5: non-numeric argument to binary operator
We can ask what type of data something is in R using the typeof() function:
typeof("valerion")[1] "character"
R set the data type of "valerion" to character, i.e. a (character) string. The earlier command failed because R can’t perform mathematical functions (“binary operator”) on vectors of type character (“non-numeric argument”).
The character data type most commonly contains letters, but anything that is placed between quotes ("...") will be interpreted as this data type – even plain numbers:
typeof("5")[1] "character"
Besides character, three other common data types are:
double/numeric– numbers that can have decimal points:typeof(3.14)[1] "double"integer– whole numbers only:typeof(1:3)[1] "integer"logical(eitherTRUEorFALSE– unquoted!):typeof(TRUE)[1] "logical"
4.2 A vector can only contain one data type
A vector can only be composed of a single data type. As we saw above, R silently picks the “best-fitting” data type when you create a vector.
The same is true when you enter or read data into a data frame, where each column is a vector and can therefore only contain one data type.
Let’s go back to our cats dataframe to find out what data types R assigned. We can quickly see each column’s data type and other information using the str() (structure) function:
str(cats)'data.frame': 3 obs. of 3 variables:
$ name : chr "Luna" "Thomas" "Daisy"
$ coat : chr "calico" "black" "tabby"
$ weight: num 2.1 5 3.2
- The
nameandcoatcolumns arecxharacter, abbreviatedchr. - The
weightcolumn isdouble/numeric, abbreviatednum.
Challenge 2
In each line below, what do you think the data type (if any) will be? Try it out and see if you were right.
typeof("TRUE")
typeof(banana)
typeof(c(2, 6, "3"))Click for the solutions
"TRUE"ischaracter(and notlogical) because of the quotes around it:typeof("TRUE")[1] "character"
Recall the earlier example: this returns an error because the object
bananadoes not exist. Any unquoted string (that is not a special keyword likeTRUEandFALSE) is interpreted as a reference to an object in R.typeof(banana)Error: object 'banana' not found
This produces a
charactervector, and we’ll talk about why in the next section:typeof(c(2, 6, "3"))[1] "character"
4.3 Automatic Type Coercion
R’s behavior of returning a character vector for c(2, 6, "3") in the challenge above is called type coercion.
When R encounters a mix of data types (here, numbers and characters) to be combined into a single vector, it forces them all to be the same type. It “must” do this because a vector can consist of only a single data type.
Type coercion can be the source of many surprises, and is one reason you need to be aware of the basic R data types and how R’s behaviour around them.
4.4 Manual Type Conversion
Luckily, you are not just at the mercy of whatever R decides to do automatically, but can convert vectors using the as. group of functions:
as.” and then press the Tab key.
as.integer(c("0", "2"))[1] 0 2
as.character(c(0, 2))[1] "0" "2"
As you may have guessed, though, not all type conversions are possible — for example:
as.double("kiwi")Warning: NAs introduced by coercion
[1] NA
(NA is R’s way of denoting missing data – see this bonus section for more.)
That’s it! In the next session, we’ll learn to manipulate and summarize data frames using a real dataset with periodic countrywise statistics such as population size.
5 Bonus material for self-study
5.1 Changing vector elements using indexing
Above, we saw how you can extract elements of a vector using indexing. To change elements in a vector, simply use the bracket on the other side of the arrow – for example:
Change the first element to
30:stepwise_vec[1] <- 30 stepwise_vec[1] 30.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0Change the last element to
0:stepwise_vec[length(stepwise_vec)] <- 0 stepwise_vec[1] 30.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 0.0Change the second element to the mean value of the vector:
stepwise_vec[2] <- mean(stepwise_vec) stepwise_vec[1] 30.000000 8.454545 6.400000 6.600000 6.800000 7.000000 7.200000 [8] 7.400000 7.600000 7.800000 0.000000
5.2 Extracting columns from a data frame
You can extract individual columns from a data frame using the $ operator:
cats$weight[1] 2.1 5.0 3.2
cats$coat[1] "calico" "black" "tabby"
This kind of operation will return a vector – and can be indexed as well:
cats$weight[2][1] 5
5.3 More on the logical data type
Add a column to your cats data frame that indicates whether each cat does or does not like string:
cats$likes_string <- c(1, 0, 1)
cats name coat weight likes_string
1 Luna calico 2.1 1
2 Thomas black 5.0 0
3 Daisy tabby 3.2 1
So, likes_string is numeric, but the 1s and 0s actually represent TRUE and FALSE.
You could instead use the logical data type here, by converting this column with the as.logical() function. That will turn 0’s into FALSE and everything else, including 1, to TRUE:
as.logical(cats$likes_string)[1] TRUE FALSE TRUE
To actually modify this column in the dataframe itself:
cats$likes_string <- as.logical(cats$likes_string)
cats name coat weight likes_string
1 Luna calico 2.1 TRUE
2 Thomas black 5.0 FALSE
3 Daisy tabby 3.2 TRUE
You might think that 1/0 could be a handier coding than TRUE/FALSE because that enables easy counting of the number of times something is true or false. But consider the following R behavior:
TRUE + TRUE[1] 2
So, logicals can be used as if they were numbers, where FALSE represents 0 and TRUE represents 1.
5.4 Missing values (NA)
R has a concept of missing data, which is important in statistical computing, as not all information/measurements are always available for each sample.
In R, missing values are coded as NA (like TRUE/FALSE, this is not a character string so it is not quoted):
# This vector will contain one missing value
vector_NA <- c(1, 3, NA, 7)
vector_NA[1] 1 3 NA 7
Notably, many functions operating on vectors will return NA if any element in the vector is NA:
sum(vector_NA)[1] NA
You can get around this is by setting na.rm = TRUE in such functions, for example:
sum(vector_NA, na.rm = TRUE)[1] 11
5.5 Factors
Categorical data, like treatments in an experiment, can be stored as “factors” in R. Factors are useful for statistical analyses and for plotting, e.g. because they allow you to specify a custom order.
diet_vec <- c("high", "medium", "low", "low", "medium")
diet_vec[1] "high" "medium" "low" "low" "medium"
factor(diet_vec)[1] high medium low low medium
Levels: high low medium
In the example above, we turned a character vector into a factor. Its “levels” (low, medium, high) are sorted alphabetically by default, but we can manually specify an order that makes more sense:
diet_fct <- factor(diet_vec, levels = c("low", "medium", "high"))
diet_fct[1] high medium low low medium
Levels: low medium high
This ordering would be automatically respected in plots and statistical analyses.
For most intents and purposes, it makes sense to think of factors as another data type, even though technically, they are a kind of data structure build on the integer data type:
typeof(diet_fct)[1] "integer"
5.6 Learn more
To learn more about data types and data structures, see this episode from a separate Carpentries lesson.
Bonus Challenge
An important part of every data analysis is cleaning input data. Here, you will clean a cat data set that has an added observation with a problematic data entry.
Start by creating the new data frame:
cats_v2 <- data.frame(
name = c("Luna", "Thomas", "Daisy", "Oliver"),
coat = c("calico", "black", "tabby", "tabby"),
weight = c(2.1, 5.0, 3.2, "2.3 or 2.4")
)Then move on to the tasks below, filling in the blanks (_____) and running the code:
# 1. Explore the data frame,
# including with an overview that shows the columns' data types:
cats_v2
_____(cats_v2)
# 2. The "weight" column has the incorrect data type _____.
# The correct data type is: _____.
# 3. Correct the 4th weight with the mean of the two given values,
# then print the data frame to see the effect:
cats_v2$weight[4] <- 2.35
cats_v2
# 4. Convert the weight column to the right data type:
cats_v2$weight <- _____(cats_v2$weight)
# 5. Calculate the mean weight of the cats:
_____Click for the solution
# 1. Explore the data frame,
# including with an overview that shows the columns' data types:
cats_v2 name coat weight
1 Luna calico 2.1
2 Thomas black 5
3 Daisy tabby 3.2
4 Oliver tabby 2.3 or 2.4
str(cats_v2)'data.frame': 4 obs. of 3 variables:
$ name : chr "Luna" "Thomas" "Daisy" "Oliver"
$ coat : chr "calico" "black" "tabby" "tabby"
$ weight: chr "2.1" "5" "3.2" "2.3 or 2.4"
# 2. The "weight" column has the incorrect data type CHARACTER.
# The correct data type is: DOUBLE/NUMERIC.
# 3. Correct the 4th weight data point with the mean of the two given values,
# then print the data frame to see the effect:
cats_v2$weight[4] <- 2.35
cats_v2 name coat weight
1 Luna calico 2.1
2 Thomas black 5
3 Daisy tabby 3.2
4 Oliver tabby 2.35
# 4. Convert the weight column to the right data type:
cats_v2$weight <- as.double(cats_v2$weight)
# 5. Calculate the mean weight of the cats:
mean(cats_v2$weight)[1] 3.1625
Footnotes
Or Click
File=>New file=>R Script.↩︎Either double quotes (
"...") or single quotes ('...') work, but the former are most commonly used by convention.↩︎R uses 1-based indexing, which means it starts counting at 1 like humans do. Index 2 therefore simply corresponds to the second element.
Python and several other languages use 0-based indexing, which starts counting at 0 such that the second element corresponds to index 1.↩︎