Note: This lecture content was originally created by voluntary contributions to Data Carpentry and has been modified to align with the aims of EEB313. Data Carpentry is an organization focused on data literacy, with the objective of teaching skills to researchers to enable them to retrieve, view, manipulate, analyze, and store their and other’s data in an open and reproducible way in order to extract knowledge from data. For EEB313, we are making all our content available under the same license, The Creative Commons, so that anyone in the future can re-use or modify our course content, without infringing on copyright licensing issues.

The above paragraph is made explicit since it is one of the core features of working with an open language like R. Many smart people willingly and actively share their material publicly, so that others can modify and build off of the material themselves.

By being open, we can “stand on the shoulders of giants” and continue to contribute for others to then stand on our shoulders. Not only does this help get work done, but it also adds to a feeling of community. In fact, there is a common saying in the open source world:

I came for the language and stayed for the community.

This saying captures the spirit, generosity, and fun involved in being a part of these open source projects.

## Lesson Preamble

### Learning Objectives

• Define the following terms as they relate to R: call, function, arguments, options.
• Use comments within code blocks.
• Do simple arithmetic operations in R using values and objects.
• Call functions and use arguments to change their default options.
• Define our own functions
• Inspect the content of vectors and manipulate their content.
• Create for-loops
• Describe what a data frame is.
• Load external data from a .csv file into a data frame in R.
• Summarize the contents of a data frame in R.

### Lecture outline

• Setting up the R Notebook (5 min)
• Creating objects/variables in R (10 min)
• Using and writing functions (20 min)
• Vectors and data types (15 min)
• Loops and vectorization (10 min)
• Dataset background (10 min)
• What are data frames (15 min)

## Setting up the R Notebook

Let’s remove the template RStudio gives us, and add a title of our own.

---
title: Introduction to R
---

This header block is called the YAML header. This is where we specify whether we want to convert this file to a HTML or PDF file. This will be discussed in more detail in another class. For now, we just care about including the lecture title here. For now, let’s type out a note:

This lecture covers fundamental R usage, such as assigning values to a variable, using functions, and commenting code, as well as some data analysis.

Under this sentence, we will insert our first code chunk. Remember that you insert a code chunk by either clicking the “Insert” button or pressing Ctrl + Alt + i simultaneously. To run a code chunk, you press the green arrow, or Ctrl + Shift + Enter.

## Creating objects in R

As we saw in our first class, you can get output from R simply by typing math in the console:

3 + 5
## [1] 8
12 / 7
## [1] 1.714286

However, to do useful and interesting things, we need to assign values to objects.

x <- 3
x + 5
## [1] 8

You can name an object in R almost anything you want:

joel <- 3
joel + 5
## [1] 8

#### Challenge

So far, we have created two variables, joel and x. What is the sum of these variables?

joel + x
## [1] 6

Objects can be given any name such as x, current_temperature, or subject_id. You want your object names to be explicit and not too long. They cannot start with a number (2x is not valid, but x2 is). R is case sensitive (e.g., joel is different from Joel). There are some names that cannot be used because they are they are reserved for fundamental functions in R (?Reserved lists these words). In general, even if it’s allowed, it’s best to not use other function names (e.g., c, T, mean, data, df, weights). If in doubt, check the help or use tab completion to see if the name is already in use.

It’s also best to avoid dots (.) within a variable name as in my.dataset. Historically, there are many functions in R with dots in their name, but since dots have a special meaning in R, it’s better to not use them and instead use underscores (_).

It is also recommended to use nouns for variable names, and verbs for function names. It’s important to be consistent in the styling of your code (where you put spaces, how you name variables, etc.). Using a consistent coding style1 makes your code clearer to read for your future self and your collaborators. RStudio will format code for you if you highlight a section of code and press Ctrl + Shift + a.

When assigning a value to an object, R does not print anything. You can force R to print the value by using parentheses or by typing the object name:

weight_kg <- 55    # doesn't print anything
(weight_kg <- 55)  # but putting parentheses around the call prints the value of weight_kg
## [1] 55
weight_kg          # and so does typing the name of the object
## [1] 55

The variable weight_kg is stored in the computer’s memory where R can access it, and we can start doing arithmetic with it efficiently. For instance, we may want to convert this weight into pounds (weight in pounds is 2.2 times the weight in kg):

2.2 * weight_kg
## [1] 121

We can also change a variable’s value by assigning it a new one:

weight_kg <- 57.5
2.2 * weight_kg
## [1] 126.5

This means that assigning a value to one variable does not change the values of other variables. For example, let’s store the animal’s weight in pounds in a new variable, weight_lb:

weight_lb <- 2.2 * weight_kg

and then change weight_kg to 100.

weight_kg <- 100

#### Challenge

What do you think is the current content of the object weight_lb? 126.5 or 220?

weight_lb
## [1] 126.5

A comment is piece of text in your code that will not be executed with the rest of the code, but its purpose is to be a note to the person reading the code. Traditionally, comments have been the only way to make notes alongside your code, but since we are using the R Notebook format, we can also write notes in Markdown. Therefore, comments are not as important for us, but they are still the best method of leaving short notes and explanations for specific lines of code. The comment character in R is #, anything to the right of a # in a script will be ignored by R.

RStudio makes it easy to comment or uncomment a paragraph: after selecting the lines you want to comment, press at the same time on your keyboard Ctrl + Shift + C. If you only want to comment out one line, you can put the cursor at any location of that line (i.e. no need to select the whole line), then press Ctrl + Shift + C.

weight_lb <- 2.2 * weight_kg # Actually, 1 kg = 2.204623 lbs

#### Challenge

What are the values after each statement in the following?

mass <- 47.5
age  <- 122
mass <- mass * 2.0      # mass?
age  <- age - 20        # age?
mass_index <- mass/age  # mass_index?

## Functions and their arguments

Functions can be thought of as recipes. You give a few ingredients as input to a function, and it will generate an output based on these ingredients. Just as when baking, both the ingredients and the actual recipe will influence what comes out of the recipe in the end, will it be a cake or a loaf of bread? In R, the inputs to a function are not called ingredients, but rather arguments, and the output is called the return value of the function. A function does not technically have to return a value, but often does so. Functions are used to automate more complicated sets of commands and many of them are already predefined in R. A typical example would be the function sqrt(). The input (the argument) must be a number, and the return value (in fact, the output) is the square root of that number. Executing a function (‘running it’) is called calling the function. An example of a function call is:

sqrt(9)
## [1] 3

Which is the same as assigning the value to a variable and then passing that variable to the function:

a <- 9
b <- sqrt(a)
b
## [1] 3

Here, the value of a is given to the sqrt() function, the sqrt() function calculates the square root, and returns the value which is then assigned to variable b. This function is very simple, because it takes just one argument.

The return ‘value’ of a function need not be numerical (like that of sqrt()), and it also does not need to be a single item: it can be a set of things, or even a dataset, as we will see later on.

Arguments can be anything, not only numbers or filenames, but also other objects. Exactly what each argument means differs per function, and must be looked up in the documentation (see below). Some functions take arguments which may either be specified by the user, or, if left out, take on a default value: these are called options. Options are typically used to alter the way the function operates, such as whether it ignores ‘bad values’, or what symbol to use in a plot. However, if you want something specific, you can specify a value of your choice which will be used instead of the default.

To access help about sqrt, we are first going to learn about tab-completion. Type s and press Tab.

s<tab>q

You can see that R gives you suggestions of what functions and variables are available that start with the letter s, and thanks to RStudio they are formatted in this nice list. There are many suggestions here, so let’s be a bit more specific and append a q, to find what we want. If we press enter or tab again, R will insert the selected option.

You can see that R inserts a pair of parenthesis together with the name of the functions. This is how the function syntax looks for R and many other programming languages, and it means that within these parentheses, we will specify all the arguments (the ingredients) that we want to pass to this function.

If we press tab again, R will helpfully display all the available parameters for this function that we can pass an argument to. The word parameter is used to describe the name that the argument can be passed to. More on that later.

sqrt(<tab>

There are many things in this list, but only one of them is marked in purple. Purple here means that it is parameter we can use for the function and yellow means that it is a variable that we defined earlier.2

To read the full help about sqrt, we can use the question mark, or type it directly into the help document browser.

?sqrt

As you can see, sqrt() takes only one argument, x, which needs to be a numerical vector. Don’t worry too much about that is says vector here, I will talk more about that later. Briefly, a numerical vector is one or more numbers. Every number is a vector, so you don’t have to do anything special to create a vector. More on vectors later.

Let’s try a function that can take multiple arguments: round().

round(<tab>
?round

If we try round with a value:

round(3.14159)
## [1] 3

Here, we’ve called round() with just one argument, 3.14159, and it has returned the value 3. That’s because the default is to round to the nearest whole number, or integer. If we want more digits we can pass an argument to the digits parameter, to specify how many decimals we want to round to.

round(3.14159, digits = 2)
## [1] 3.14

So, above we pass the argument 2, to the parameter digits. Knowing this nomenclature is not essential for doing your own data analysis, but it will be very helpful when you are reading through help documents online and in RStudio.

We can leave out the word digits since we know it comes as the second parameter, after x.

round(3.14159, 2)
## [1] 3.14

As you notice, we have been leaving out x from the beginning. If you provide the names for both the arguments, we can switch their order:

round(digits = 2, x = 3.14159)
## [1] 3.14

It’s good practice to put the non-optional arguments (like the number you’re rounding) first in your function call, and to specify the names of all optional arguments. If you don’t, someone reading your code might have to look up the definition of a function with unfamiliar arguments to understand what you’re doing.

## Writing functions

In this class, you will be working a lot with functions, especially those that someone else has already written. When you type sum, c(), or mean(), you are using a function that has been made previously and built into R. To remove some of the magic around these functions, we will go through how to make a basic function of our own. Let’s start with a simple example where we add two numbers together:

add_two_numbers <- function(num1, num2) {
return(num1 + num2)
}
add_two_numbers(4, 5)
## [1] 9

As you can see, running this function on two numbers returns their sum. We could also assign to a variable in the function and return the function.

add_two_numbers <- function(num1, num2) {
my_sum <- num1 + num2
return(my_sum)
}
add_two_numbers(4, 5)
## [1] 9

#### Challenge

Can you write a function that calculates the mean of 3 numbers?

mean_of_three_numbers <- function(num1, num2, num3) {
my_sum <- num1 + num2 + num3
my_mean <- my_sum / 3
return(my_mean)
}
mean_of_three_numbers(2, 4, 6)
## [1] 4

## Vectors and data types

A vector is the most common and basic data type in R, and is pretty much the workhorse of R. A vector is composed by a series of values, which can be either numbers or characters. We can assign a series of values to a vector using the c() function, which stands for “concatenate (combine/connect one after another) values into a vector” For example we can create a vector of animal weights and assign it to a new object weight_g:

weight_g <- c(50, 60, 65, 82) # Concatenate/Combine values into a vector
weight_g
## [1] 50 60 65 82

You can also use the built-in command seq, to create a sequence of numbers without typing all of them in manually.

seq(0, 30) # This is the same as just 0:30
##  [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
## [24] 23 24 25 26 27 28 29 30
seq(0, 30, 3) # Every third number
##  [1]  0  3  6  9 12 15 18 21 24 27 30

A vector can also contain characters:

animals <- c('mouse', 'rat', 'dog')
animals
## [1] "mouse" "rat"   "dog"

The quotes around “mouse”, “rat”, etc. are essential here and can be either single or double quotes. Without the quotes R will assume there are objects called mouse, rat and dog. As these objects don’t exist in R’s memory, there will be an error message.

There are many functions that allow you to inspect the content of a vector. length() tells you how many elements are in a particular vector:

length(weight_g)
## [1] 4
length(animals)
## [1] 3

An important feature of a vector is that all of the elements are the same type of data. The function class() indicates the class (the type of element) of an object:

class(weight_g)
## [1] "numeric"
class(animals)
## [1] "character"

The function str() provides an overview of the structure of an object and its elements. It is a useful function when working with large and complex objects:

str(weight_g)
##  num [1:4] 50 60 65 82
str(animals)
##  chr [1:3] "mouse" "rat" "dog"

You can use the c() function to add other elements to your vector:

weight_g <- c(weight_g, 90) # add to the end of the vector
weight_g <- c(30, weight_g) # add to the beginning of the vector
weight_g
## [1] 30 50 60 65 82 90

In the first line, we take the original vector weight_g, add the value 90 to the end of it, and save the result back into weight_g. Then we add the value 30 to the beginning, again saving the result back into weight_g.

We can do this over and over again to grow a vector, or assemble a dataset. As we program, this may be useful to add results that we are collecting or calculating.

An atomic vector is the simplest R data type and it is a linear vector of a single type, e.g. all numbers. Above, we saw 2 of the 6 main atomic vector types that R uses: "character" and "numeric" (or "double"). These are the basic building blocks that all R objects are built from. The other 4 atomic vector types are:

• "logical" for TRUE and FALSE (the boolean data type)
• "integer" for integer numbers (e.g., 2L, the L indicates to R that it’s an integer)
• "complex" to represent complex numbers with real and imaginary parts (e.g., 1 + 4i) and that’s all we’re going to say about them
• "raw" for bitstreams that we won’t discuss further

Vectors are one of the many data structures that R uses. Other important ones are lists (list), matrices (matrix), data frames (data.frame), factors (factor) and arrays (array). In this class, we will focus on data frames, which is most commonly used one for data analyses.

#### Challenge

• We’ve seen that atomic vectors can be of type character, numeric (or double), integer, and logical. But what happens if we try to mix these types in a single vector? Find out by using class to test these examples.
num_char <- c(1, 2, 3, 'a')
num_logical <- c(1, 2, 3, TRUE)
char_logical <- c('a', 'b', 'c', TRUE)
tricky <- c(1, 2, 3, '4')
# Answer
class(num_char)
## [1] "character"
class(num_logical)
## [1] "numeric"
class(char_logical)
## [1] "character"
class(tricky)
## [1] "character"
• This happens because vectors can be of only one data type. Instead of throwing an error and saying that you are trying to mix different types in the same vector, R tries to convert (coerce) the content of this vector to find a “common denominator”. A logical can be turn into 1 or 0, and a number can be turned into a string/character representation. It would be difficult to do it the other way around: would 5 be TRUE or FALSE? What number would ‘t’ be?

• In R, we call converting objects from one class into another class coercion. These conversions happen according to a hierarchy, whereby some types get preferentially coerced into other types. Can you draw a diagram that represents the hierarchy of how these data types are coerced?

# Answer:
# logical -> numeric -> character <- logical

## Loops and vectorization

Loop, or for-loops, are essential to programming in general. However, in R, you should avoid them as often as possible because there are more efficient ways of doing things that you should use instead. It is still important that you understand the concept of loops and you might also use them in some of your own functions if there is no vectorized way of going about what you want to do.

You can think of a for-loop as: “for each number contained in a list/vector, perform this operation” and the syntax basically says the same thing:

v <- c(2, 4, 6)
for (num in v) {
print(num)
}
## [1] 2
## [1] 4
## [1] 6

Instead of printing out every number to the console, we could also add numbers cumulatively, to calculate the sum of all the numbers in the vector:

# To increment w each time, we must first create the variable,
# which we do by setting w <- 0, referred to as initializing.
# This also ensures that w is zero at the start of the loop and
# doesn't retain the value from last time we ran this code.
w <- 0
for (num in v) {
w <- w + num
}
w
## [1] 12

If we put what we just did inside a function, we have essentially recreated the sum function in R.

my_sum <- function(input_vector) {
vector_sum <- 0
for (num in input_vector){
vector_sum <- vector_sum + num
}
return(vector_sum)
}

my_sum(v)
## [1] 12

Although this gives us the same output as the built-in function sum, the built-in function has many more optimizations so it is much faster than our function. In R, it is always faster to try to find a way of doing things without writing a loop yourself. When you are reading about R, you might see suggestions that you should try to vectorize your code to make it faster. What people are referring to, is that you should not write for loops in R and instead use the ready-made functions that are much more efficient in working with vectors and essentially performs operations on entire vector at once instead of one number at a time. If anyone is interested in more details about how this works, please ask me after class, but conceptually loops operate on one element at a time while vectorized code operates on all elements of a vector at once.

## Working with real data - dataset background

Today, we will begin examining real data from a longitudinal study of the species abundance in the Chihuahuan desert ecosystem near Portal, Arizona, USA. This study includes observations of plants, ants, and rodents from 1977 - 2002, and has been used in over 100 publications. More information is available in the abstract of this paper from 2009.

We will be working with this dataset over the next few lectures. There are several datasets available related to this study, and we will be working with datasets that have been preprocessed by the Data Carpentry to facilitate teaching. These are made available online as The Portal Project Teaching Database, both at the Data Carpentry website, and on Figshare. Figshare is a great place to publish data, code, figures, and more openly to make them available for other researchers and to communicate findings that are not part of a longer paper.

### Presentation of the survey data

We are studying the species and weight of animals caught in plots in our study area. The dataset is stored as a comma separated value (CSV) file. Each row holds information for a single animal, and the columns represent:

Column Description
record_id unique id for the observation
month month of observation
day day of observation
year year of observation
plot_id ID of a particular plot
species_id 2-letter code
sex sex of animal (“M”, “F”)
hindfoot_length length of the hindfoot in mm
weight weight of the animal in grams
genus genus of animal
species species of animal
taxa e.g. rodent, reptile, bird, rabbit
plot_type type of plot

To read the data into R, we are going to use a function called read_csv. This function is contained in an R-package called readr. R-packages are a bit like browser extensions; they are not essential, but can provide nifty functionality. We will go through R-packages in general and which ones are good for data analyses in detail later in this lecture. Now, let’s install readr:

install.packages('readr') # You only need to install a package once

Now we can use the read_csv function. One useful option that read_csv includes, is the ability to read a CSV file directly from a URL, without downloading it in a separate step:

surveys <- readr::read_csv('https://ndownloader.figshare.com/files/2292169')

However, it is often a good idea to download the data first, so you have a copy stored locally on your computer in case you want to do some offline analyses, or the online version of the file changes or the file is taken down. You can either download the data manually or from within R:

download.file("https://ndownloader.figshare.com/files/2292169",
"portal_data.csv") # Saves this name in the current directory

The data is read in by specifying its local path.

surveys <- readr::read_csv('portal_data.csv')
## Parsed with column specification:
## cols(
##   record_id = col_double(),
##   month = col_double(),
##   day = col_double(),
##   year = col_double(),
##   plot_id = col_double(),
##   species_id = col_character(),
##   sex = col_character(),
##   hindfoot_length = col_double(),
##   weight = col_double(),
##   genus = col_character(),
##   species = col_character(),
##   taxa = col_character(),
##   plot_type = col_character()
## )

This statement produces some output regarding which data type it found in each column. If we want to check this in more detail, we can print the variable’s value: surveys.

surveys
## # A tibble: 34,786 x 13
##    record_id month   day  year plot_id species_id sex   hindfoot_length
##        <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>      <chr>           <dbl>
##  1         1     7    16  1977       2 NL         M                  32
##  2        72     8    19  1977       2 NL         M                  31
##  3       224     9    13  1977       2 NL         <NA>               NA
##  4       266    10    16  1977       2 NL         <NA>               NA
##  5       349    11    12  1977       2 NL         <NA>               NA
##  6       363    11    12  1977       2 NL         <NA>               NA
##  7       435    12    10  1977       2 NL         <NA>               NA
##  8       506     1     8  1978       2 NL         <NA>               NA
##  9       588     2    18  1978       2 NL         M                  NA
## 10       661     3    11  1978       2 NL         <NA>               NA
## # … with 34,776 more rows, and 5 more variables: weight <dbl>,
## #   genus <chr>, species <chr>, taxa <chr>, plot_type <chr>

In the online html-version of this lecture, you only see the first few rows of the data frame. Running the code chunk above in the R Notebook would display a nice tabular view of the data, which also includes pagination when there are many rows and we can click the green arrow to view all the columns. Technically, this object actually a tibble rather than a data frame, as indicated in the output. The reason for this is that read_csv automatically converts the data into to a tibble when loading it. Since a tibble is just a data frame with some convenient extra functionality, we will use these words interchangeably from now on.

If we just want to glance at how the data frame looks, it is sufficient to display only the top (the first 6 lines) using the function head():

head(surveys)
## # A tibble: 6 x 13
##   record_id month   day  year plot_id species_id sex   hindfoot_length
##       <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>      <chr>           <dbl>
## 1         1     7    16  1977       2 NL         M                  32
## 2        72     8    19  1977       2 NL         M                  31
## 3       224     9    13  1977       2 NL         <NA>               NA
## 4       266    10    16  1977       2 NL         <NA>               NA
## 5       349    11    12  1977       2 NL         <NA>               NA
## 6       363    11    12  1977       2 NL         <NA>               NA
## # … with 5 more variables: weight <dbl>, genus <chr>, species <chr>,
## #   taxa <chr>, plot_type <chr>

## What are data frames?

Data frames are the de facto data structure for most tabular data, and what we use for statistics and plotting. A data frame can be created by hand, but most commonly they are generated by the function read_csv(). In other words, when importing spreadsheets from your hard drive (or the web).

A data frame is the representation of data in the format of a table where the columns are vectors that all have the same length. Because the columns are vectors, they all contain the same type of data as we discussed before the break (e.g., characters, integers, factors). We can see this when inspecting the structure of a data frame with the function str():

str(surveys)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 34786 obs. of  13 variables:
##  $record_id : num 1 72 224 266 349 363 435 506 588 661 ... ##$ month          : num  7 8 9 10 11 11 12 1 2 3 ...
##  $day : num 16 19 13 16 12 12 10 8 18 11 ... ##$ year           : num  1977 1977 1977 1977 1977 ...
##  $plot_id : num 2 2 2 2 2 2 2 2 2 2 ... ##$ species_id     : chr  "NL" "NL" "NL" "NL" ...
##  $sex : chr "M" "M" NA NA ... ##$ hindfoot_length: num  32 31 NA NA NA NA NA NA NA NA ...
##  $weight : num NA NA NA NA NA NA NA NA 218 NA ... ##$ genus          : chr  "Neotoma" "Neotoma" "Neotoma" "Neotoma" ...
##  $species : chr "albigula" "albigula" "albigula" "albigula" ... ##$ taxa           : chr  "Rodent" "Rodent" "Rodent" "Rodent" ...
##  $plot_type : chr "Control" "Control" "Control" "Control" ... ## - attr(*, "spec")= ## .. cols( ## .. record_id = col_double(), ## .. month = col_double(), ## .. day = col_double(), ## .. year = col_double(), ## .. plot_id = col_double(), ## .. species_id = col_character(), ## .. sex = col_character(), ## .. hindfoot_length = col_double(), ## .. weight = col_double(), ## .. genus = col_character(), ## .. species = col_character(), ## .. taxa = col_character(), ## .. plot_type = col_character() ## .. ) Integer refers to a whole number, such as 1, 2, 3, 4, etc. Numbers with decimals, 1.0, 2.4, 3.333, are referred to as floats. Factors are used to represent categorical data. Factors can be ordered or unordered, and understanding them is necessary for statistical analysis and for plotting. Factors are stored as integers, and have labels (text) associated with these unique integers. While factors look (and often behave) like character vectors, they are actually integers under the hood, and you need to be careful when treating them like strings. ### Inspecting data.frame objects We already saw how the functions head() and str() can be useful to check the content and the structure of a data frame. Here is a non-exhaustive list of functions to get a sense of the content/structure of the data. Let’s try them out! • Size: • dim(surveys) - returns a vector with the number of rows in the first element and the number of columns as the second element (the dimensions of the object) • nrow(surveys) - returns the number of rows • ncol(surveys) - returns the number of columns • Content: • head(surveys) - shows the first 6 rows • tail(surveys) - shows the last 6 rows • Names: • names(surveys) - returns the column names (synonym of colnames() for data.frame objects) • rownames(surveys) - returns the row names • Summary: • str(surveys) - structure of the object and information about the class, length, and content of each column • summary(surveys) - summary statistics for each column Note: most of these functions are “generic”, they can be used on other types of objects besides data.frame. #### Challenge Based on the output of str(surveys), can you answer the following questions? • What is the class of the object surveys? • How many rows and how many columns are in this object? • How many species have been recorded during these surveys? ## Answers ## ## * class: data frame ## * how many rows: 34786, how many columns: 13 ## * how many species: 48 ### Indexing and subsetting data frames Our survey data frame has rows and columns (it has 2 dimensions), if we want to extract some specific data from it, we need to specify the “coordinates” we want from it. Row numbers come first, followed by column numbers. Base R data frames, would return a different format depending on how we index the data (i.e. either a vector or a data frame), but with enhanced data frames, tibbles, the returned object is almost always a data frame. R indices start at 1. Programming languages like Fortran, MATLAB, Julia, and R start counting at 1, because that’s what human beings typically do. Languages in the C family (including C++, Java, Perl, and Python) count from 0 because that was historically simpler for computers and can allow for more elegant code. surveys[1, 1] # first element in the first column of the data frame ## # A tibble: 1 x 1 ## record_id ## <dbl> ## 1 1 surveys[1, 6] # first element in the 6th column ## # A tibble: 1 x 1 ## species_id ## <chr> ## 1 NL surveys[, 1] # first column in the data frame ## # A tibble: 34,786 x 1 ## record_id ## <dbl> ## 1 1 ## 2 72 ## 3 224 ## 4 266 ## 5 349 ## 6 363 ## 7 435 ## 8 506 ## 9 588 ## 10 661 ## # … with 34,776 more rows surveys[1] # first column in the data frame ## # A tibble: 34,786 x 1 ## record_id ## <dbl> ## 1 1 ## 2 72 ## 3 224 ## 4 266 ## 5 349 ## 6 363 ## 7 435 ## 8 506 ## 9 588 ## 10 661 ## # … with 34,776 more rows surveys[1:3, 7] # first three elements in the 7th ## # A tibble: 3 x 1 ## sex ## <chr> ## 1 M ## 2 M ## 3 <NA> surveys[3, ] # the 3rd element for all columns ## # A tibble: 1 x 13 ## record_id month day year plot_id species_id sex hindfoot_length ## <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> ## 1 224 9 13 1977 2 NL <NA> NA ## # … with 5 more variables: weight <dbl>, genus <chr>, species <chr>, ## # taxa <chr>, plot_type <chr> surveys[1:6, ] # equivalent to head(surveys) ## # A tibble: 6 x 13 ## record_id month day year plot_id species_id sex hindfoot_length ## <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> ## 1 1 7 16 1977 2 NL M 32 ## 2 72 8 19 1977 2 NL M 31 ## 3 224 9 13 1977 2 NL <NA> NA ## 4 266 10 16 1977 2 NL <NA> NA ## 5 349 11 12 1977 2 NL <NA> NA ## 6 363 11 12 1977 2 NL <NA> NA ## # … with 5 more variables: weight <dbl>, genus <chr>, species <chr>, ## # taxa <chr>, plot_type <chr> : is a special function that creates numeric vectors of integers in increasing or decreasing order, test 1:10 and 10:1 for instance. This works similar to seq, which we looked at earlier in class: 0:10 ## [1] 0 1 2 3 4 5 6 7 8 9 10 seq(0, 10) ## [1] 0 1 2 3 4 5 6 7 8 9 10 # We can test if all elements are the same 0:10 == seq(0,10) ## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE all(0:10 == seq(0,10)) ## [1] TRUE You can also exclude certain parts of a data frame using the “-” sign: surveys[,-1] # All columns, except the first ## # A tibble: 34,786 x 12 ## month day year plot_id species_id sex hindfoot_length weight genus ## <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <chr> ## 1 7 16 1977 2 NL M 32 NA Neot… ## 2 8 19 1977 2 NL M 31 NA Neot… ## 3 9 13 1977 2 NL <NA> NA NA Neot… ## 4 10 16 1977 2 NL <NA> NA NA Neot… ## 5 11 12 1977 2 NL <NA> NA NA Neot… ## 6 11 12 1977 2 NL <NA> NA NA Neot… ## 7 12 10 1977 2 NL <NA> NA NA Neot… ## 8 1 8 1978 2 NL <NA> NA NA Neot… ## 9 2 18 1978 2 NL M NA 218 Neot… ## 10 3 11 1978 2 NL <NA> NA NA Neot… ## # … with 34,776 more rows, and 3 more variables: species <chr>, ## # taxa <chr>, plot_type <chr> surveys[-c(7:34786),] # Equivalent to head(surveys) ## # A tibble: 6 x 13 ## record_id month day year plot_id species_id sex hindfoot_length ## <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> ## 1 1 7 16 1977 2 NL M 32 ## 2 72 8 19 1977 2 NL M 31 ## 3 224 9 13 1977 2 NL <NA> NA ## 4 266 10 16 1977 2 NL <NA> NA ## 5 349 11 12 1977 2 NL <NA> NA ## 6 363 11 12 1977 2 NL <NA> NA ## # … with 5 more variables: weight <dbl>, genus <chr>, species <chr>, ## # taxa <chr>, plot_type <chr> As well as using numeric values to subset a data.frame (or matrix), columns can be called by name, using one of the four following notations: surveys["species_id"] # Result is a data.frame ## # A tibble: 34,786 x 1 ## species_id ## <chr> ## 1 NL ## 2 NL ## 3 NL ## 4 NL ## 5 NL ## 6 NL ## 7 NL ## 8 NL ## 9 NL ## 10 NL ## # … with 34,776 more rows surveys[, "species_id"] # Result is a data.frame ## # A tibble: 34,786 x 1 ## species_id ## <chr> ## 1 NL ## 2 NL ## 3 NL ## 4 NL ## 5 NL ## 6 NL ## 7 NL ## 8 NL ## 9 NL ## 10 NL ## # … with 34,776 more rows For our purposes, these notations are equivalent. RStudio knows about the columns in your data frame, so you can take advantage of the autocompletion feature to get the full and correct column name. Another syntax that is often used to specify column names is $. In this case, the returned object is actually a vector. We will not go into detail about this, but since it is such common usage, it is good to be aware of this.

# We use head() since the output from vectors are not automatically cut off
# and we don't want to clutter the screen with all the species_id values
head(surveys\$species_id)          # Result is a vector
## [1] "NL" "NL" "NL" "NL" "NL" "NL"

#### Challenge

1. Create a data.frame (surveys_200) containing only the observations from row 200 of the surveys dataset.

2. Notice how nrow() gave you the number of rows in a data.frame?

• Use that number to pull out just that last row in the data frame.
• Compare that with what you see as the last row using tail() to make sure it’s meeting expectations.
• Pull out that last row using nrow() instead of the row number.
• Create a new data frame object (surveys_last) from that last row.
3. Use nrow() to extract the row that is in the middle of the data frame. Store the content of this row in an object named surveys_middle.

4. Combine nrow() with the - notation above to reproduce the behaviour of head(surveys) keeping just the first through 6th rows of the surveys dataset.

## Answers
surveys_200 <- surveys[200, ]
surveys_last <- surveys[nrow(surveys), ]
surveys_middle <- surveys[nrow(surveys)/2, ]
surveys_head <- surveys[-c(7:nrow(surveys)),]

In our next lecture, we will dive into wrangling our surveys dataset using dplyr.

1. Refer to the class resources page for which style to adhere to.

2. There are a few other symbols as well, all of which can be viewed at the end of this post about RStudio code completion.