Lab Activity 1
Lab Activity 1
Millie Murphy
2025-02-06
The code above is very important. It sets the working directory and calls
upon the location of the datasets on my computer. You will need to change
this to match your computer.
Student Learning Outcomes
Learning how to start with R and RStudio
Use functions in R
Data, R scripts, and other resources for this lab is found on Moodle.
Learning the tools
What is R?
R is a computer program that allows an extraordinary range of statistical
calculations. It is a free program, mainly written by voluntary contributions
from statisticians around the world. R is available on most operating
systems, including Windows, Mac OS, and Linux.
R can make graphics and do statistical calculations. It is also a full-fledged
computing language. In this series of labs, we will only scratch the surface of
what R can do.
What is RStudio?
RStudio is a separate program, also free, that provides a more elegant front
end for R. RStudio allows you to easily organize separate windows for R
commands, graphic, help, etc. in one place.
Getting started
By this point, you should have already downloaded R and RStudio. When you
start RStudio, it will automatically start R as well. You run R inside RStudio.
After you have started RStudio, you should see a new window with a menu
bar at the top and three main sections. One of the sections is called the
“Console” – this is where you type commands to give instructions to R and
typically where you see R’s answers to you.
Another important corner of this window can show a variety of information.
Most importantly to us, this is where graphics will appear, under the tab
marked “Plots”.
The command line
When you start RStudio, you’ll see a corner of the window called the
“Console.” By the default the console window is in the bottom left of the
RStudio screen.
You can type commands in this window where there is a prompt (which will
look like a > sign at the bottom of the window). The Console has to be the
selected window. (Clicking anywhere in the Console selects it.)
The > prompt is R’s way of inviting you to give it instructions. You
communicate with R by typing commands after the > prompt.
Type “2+2” at the > prompt, and hit return. You’ll see that R can work like a
calculator (among its many other powers). It will give you the answer, 4, and
it will label that answer with [1] to indicate that it is the first element in the
answer. (This is sort of annoying when the answers are simple like this, but
can be very valuable when the answers become more complex.)
In these labs, the input will show up in a gray box and the output, if any, will
follow in a white box.
2+2
## [1] 4
log()
You can use a wide variety of math functions to make calculations here, e.g.,
log() calculates the log of a number:
log(42)
## [1] 3.73767
(By default, this gives the natural log with base e.)
Parentheses are used both as a way to group elements of the calculation and
also as a way to denote the arguments of functions. (The “arguments” of a
function are the set of values given to it as input.) For example, log(3) is
applying the function log() to the argument 3.
sqrt()
Another mathematical function that often comes in handy is the square root
function, sqrt(). For example, the square root of 4 is:
sqrt(4)
## [1] 2
## [1] 64
## [1] 0.09798692
Functions
Most of the work in R is done by functions. A function has a name and one or
more arguments. For example, log(4) is a function that calculates the log in
base e for the value 4 given as input.
Sometimes functions have optional input arguments. For the function log(),
for example, we can specify the optional input argument base to tell the
function what base to use for the logarithm. If we don’t specify the base
variable, it has a default value of base = e. To get a log in base 10, for
example, we would use:
log(4, base = 10)
## [1] 0.60206
Defining objects
In R, we can store information of various sorts by assigning them to objects.
For example, if we want to create an object called x and give it a value of 4,
we would write
x <- 4
The middle bit of this—a less than sign and a hyphen typed together to make
something that looks a little like a left-facing arrow—tells R to assign the
value on the right to the object on the left. After running the command
above, whenever we use x in a command it would be replaced by its value 4.
For example, if we add 3 to x, we would expect to get 7.
x+3
## [1] 7
Objects in R can store more than just simple numbers. They can store lists of
numbers, functions, graphics, etc., depending on what values get assigned
to the object.
We can always reassign a new value to an object. If we now tell R that x is
equal to 32:
x <- 32
## [1] 32
Names
Naming objects and functions in R is pretty flexible.
A name has to start with a letter, but that can be followed by letters or
numbers. There can’t be any spaces, though.
Names in R are case-sensitive, which means that Weights and weights are
completely different things to R. This is a common and incredibly frustrating
source of errors in R.
It’s a good idea to have your names be as descriptive as possible, so that
you will know what you meant later on when looking at it. (However, if they
get too long, it becomes painful and error prone to type them each time we
use them, so this, as with all things, requires moderation.)
Sometimes clear naming means that it is best to have multiple words in the
name, but we can’t have spaces. Therefore, a common approach is like we
saw in the previous section, to chain the words with underscores (not
hyphens!), as in weights_before_hospital. (Another solution to make
separate words stand out in an object name is to vary the case:
weightsBeforeHospital.)
R Command Summary
Questions
1. For each week, create an R script that captures the commands that
you use to answer the questions. Use a # at the beginning of each
comment line.
a. Open a new R script file. Start by adding comments with your name
and the week (Week 1) at the top.
#Ignore this question since we are working in R Markdown.
2. All of the commands used in this lab are in a script called “R Lab 1 –
Part 1 Script” on Moodle. (A similar file will be available for all R
activities in this class.)
b. Run most or all of the commands in R. Did you get the same
answers as shown in this handout?
In the formula above, pi is 3.1415…., and the exp() function raises the base
of the natural log (e = 2.78…) to the power of the value in the argument.
3. For each of the following, come up with an object name that would be
appropriate to use in R for the listed variable:
a. Body temperature in Celsius
4. #bodytemp_c
8. Use R to calculate:
a. 15 x 17
15 * 17
## [1] 255
b. 13^3
13^3
## [1] 2197
## [1] 2.639057
## [1] 2
e. √81.
sqrt(81)
## [1] 9
Use vectors
If you have not already done so, download the data files, R scripts and other
resources for Part 2 of R Lab 1 – An Introduction to R.
Vectors
One useful feature of R is the ability to sometimes apply functions to an
entire collection of numbers. The technical term for a set of numbers is
“vector”. For example, the following code will create a vector of five
numbers:
c(78, 85, 64, 54, 102, 98.6)
c()
c() is a function that creates a vector, containing the list of items given in its
arguments. To help you remember, you could think of the function c()
meaning to “combine” some elements into a vector.
Let’s add a little extra here to make the computer remember this vector.
Let’s assign it to a object, called temperatureF (because these numbers are
actually a set of temperatures in degrees Fahrenheit):
temperatureF <- c(78, 85, 64, 54, 102, 98.6)
The combination of the less than sign and the hyphen makes an arrow
pointing from right to left—this tells R to assign the stuff on the right to the
name on the left. In this case we are assigning a vector to the object
temperatureF.
Inputting this to R causes no obvious output, but R will now remember this
vector of temperatures under the name temperatureF. We can view the
contents of the vector temperatureF by simply typing its name:
temperatureC <- (temperatureF - 32) * 5/9
temperatureC
To pull out one of the numbers in this vector, we add square brackets after
the vector name, and inside those brackets put the index of the element we
want. (The “index” is just a number giving the relative location in the vector
of the item we want. The first item has index 1, etc.) For example, the
second element of the vector temperatureC is:
temperatureC[2]
## [1] 29.44444
The result will be a vector that has each patient’s change in weight. (Note
that the code above is as a comment. This allows R Markdown to run without
an error.)
Basic calculation examples
In this course, we’ll learn how to use a few dozen functions, but let’s start
with a couple of basic ones.
mean()
The function mean() does just what it sounds like: it calculates the sample
mean (that is, the average) of the vector given to it as input. For example,
the mean of the vector of the temperatures in degrees Celsius from above is
26.81481:
mean(temperatureC)
## [1] 26.81481
sum()
Another simple (and simply named) function calculates the sum of all
numbers in a vector: sum().
sum(temperatureC)
## [1] 160.8889
length()
To count the number of elements in a vector, use length().
length(temperatureC)
## [1] 6
This shows that there are 6 temperature values in the vector that make up
the vector temperatureC.
Expanding R’s capabilities
R has a lot of power in its basic form, but one of the most important parts
about R is that it is expandable by the work of other people. These
expansions are usually released in “packages”.
Each package needs to be installed on your computer only once, but to be
used it has to be loaded into R during each session.
To install a package in RStudio, click on the packages tab from the sub-
window with tables for Files, Plots, Packages, Help, and Viewer. Immediately
below that will be a button labeled “Install”—click that and a window will
open.
This only needs to be done once on a given computer, and that package is
permanently available.
There is another package that we will use in this course (including today)
called dplyr. While you are installing packages, go ahead and install dplyr
as well.
Loading a package
Once a package is installed, it needs to be loaded into R during a session if
you want to use it. You do this with a function called library().
library()
For this lab, we will use the package dplyr, which allows for easy
modification of data frames. Before using the functions in this package, we
need to load it. We do this with the library() function. In the console, enter
this:
library(dplyr)
If the dplyr package is installed on your computer, the computer will just
give a new prompt and be ready to go. If the package is not installed it will
give you an error message in red asking you to get the package installed.
(See the section above.)
Setting the working directory
The files on your computers are organized hierarchically into folders, or
“directories.” It is convenient in RStudio to tell R which directory to look for
files at the beginning of a session, to minimize typing later.
You can set the working directory for RStudio from RStudio’s menu. From the
“Session” tab in the menu bar, choose “Set Working Directory”, and then
“Choose Directory…” This will open a dialog box that will let you find and
select the directory you want. For this course, you may want to keep all the
files associated with R in the same folder on your computer. (Note that this
was done at the top of this document. Doing this allows the code that follows
to work, allowing the dataset to be loaded.)
Reading a file
In these labs, we have saved the data in a “comma-separated variable”
format. All files in this format ought to have “.csv” as the end of their file
name. A CSV file is a plain text file, easily read by a wide variety of
programs. Each row in the file (besides the first row) is the data for a given
individual, and for each individual each variable is listed in the same order,
separated by commas. It’s important to note that you can’t have commas
anywhere else in the file, besides the separators.
The first row of a CSV file should be a “header” row, which gives the names
of each variable, again separated by commas.
read.csv()
For examples in this lab, let’s use a data set about the passengers of the
RMS Titanic. One of the data sets in the folder of data attached to this lab is
called “titanic.csv”. This is a data set of 1313 passengers from the voyage of
this ship, which contains information about some personal info about each
passenger as well as whether they survived the accident or not.
To import a CSV file into R, use the read.csv() function as in the following
command. (This assumes that you have set the working directory to a
specific folder on your computer.)
setwd("~/Desktop/Bio 341")
titanicData <- read.csv("titanic.csv", stringsAsFactors = TRUE)
This looks for the file called titanic.csv in the folder called DataForLabs. Here
we have given the name titanicData to the object in R that contains all this
passenger data. Of course, if you wanted to load a different data set, you
would be better off giving it a more apt name than “titanicData”. The option
“stringsAsFactors = TRUE” asks R to interpret the columns with non-
numerical information as “factor” with possibly repeated instances of the
same value of a categorical variable.
To see if the data loads appropriately, you might want to run the command
summary(titanicData)
which will list all the variables and some summary statistics for each
variable.
R has other functions that can read other data formats besides csv files, but
the function read.csv() requires that the file be a csv file.
Finding files in other locations
For all of the examples in these labs, we have assumed that the data is in a
folder called DataForLabs. When you work on a new data set outside of these
labs, you will want to store the data somewhere else. To upload data from
another location on your computer, you need to know the file path for the
file. For example, the file path to the titanic file on my computer is “C:\Users\
davisjg\Desktop\Wofford Courses\BIO341\DataForLabs\titanic.csv”. This file
path is a list of folders inside of folders that tells the computer where to look
for the file.
Reading a file from any location can be done with read.csv like this:
#titanicData <- read.csv("C:\Users\davisjg\Desktop\Wofford Courses\
BIO341\DataForLabs\titanic.csv", stringsAsFactors = TRUE)
(Note that I have already loaded the dataset in this R Markdown file so the
code above is stated as a comment so that it is ignored.)
Introduction to data frames
A data frame is a way that R can store a data set on a number of individuals.
A data frame is a collection of columns; each column contains the values of a
single variable for all individuals. The values of each individual occur in the
same order in all the columns, so the first value for one variable represents
the same individual as the first value in the lists of all other variables.
The function read.csv() loads the data it reads into a data frame.
The data frame is usually given a name, which is used to tell R’s functions
which data set to use. For example, in the previous section we read in a data
set to a data frame that we called titanicData. This data frame now contains
information about each of the passengers on the Titanic. This data frame has
seven variables, so it has seven columns (passenger_class, name, age,
embarked, home_destination, sex, and survive).
Very importantly, we can grab one of the columns from a data frame by
itself. We write the name of the data frame, followed by a $, and then the
name of the variable.
For example, to show a list of the age of all the passengers on the Titanic,
use
titanicData$age
This will show a vector that has all the values for this variable age, one for
each individual in the data set.
Adding a new column
Sometimes we would like to add a new column to a data frame. The easiest
way to do this is to simply assign a new vector to a new column name, using
the $.
For example, to add the log of age as a column in the titanicData data
frame, we can write
titanicData$log_age = log(titanicData$age)
You can run the command head(titanicData) to see that log_age is now a
column in titanicData.
head(titanicData)
In the titanic data set there is a variable named sex, and an individual is
female if that variable has value “female”. We can create a new data frame
that includes only the data from females with the following command:
titanicDataFemalesOnly <- filter(titanicData, sex == "female")
This new data frame will include all the same columns as the original
titanicData, but it will only include the rows for which the sex was “female”.
Note that the syntax here requires a double == sign. In R (and many other
computer languages), the double equal sign creates a statement that can be
evaluated as true or false, while a single equal sign may change the value of
the object to the value on the right-hand side of the equal sign. Here we are
asking, for each individual, whether sex is “female”, not assigning the value
”female” to the variable sex. So we use a double equal sign ==.
R Commands Summary
## [1] 420
d. What is the mean concentration of urine per liter? How did this change
relative to the mean measurement of ng ACE / L ?
mean_urine_concentration <- mean(ACE_concentration_liters)
## [1] 0.105
## [1] 52500
2. Weddell seals live in Antarctic waters and take long strenuous dives in
order to find fish to feed upon. Researchers (Williams et al. 2004)
wanted to know whether these feeding dives were more energetically
expensive than regular dives (perhaps because they are deeper, or the
seal has to swim further or faster). They measured the metabolic costs
of dives using the oxygen consumption of 10 animals (in ml O2 / kg)
during a feeding dive. Here are the data:
71.0, 77.3, 82.6, 96.1, 106.6, 112.8, 121.2, 126.4, 127.5, 143.1
For the same 10 animals, they also measured the oxygen consumption in
non-feeding dives. With the 10 animals in the same order as before, here are
those data:
42.2, 51.7, 59.8, 66.5, 81.9, 82.0, 81.3, 81.3, 96.0, 104.1
a. Make a vector for each of these lists, and give them appropriate
names.
oxygen_consumption_feeding <- c(71.0, 77.3, 82.6, 96.1, 106.6, 112.8,
121.2, 126.4, 127.5, 143.1)
oxygen_consumption_non_feeding <- c(42.2, 51.7, 59.8, 66.5, 81.9,
82.0, 81.3, 81.3, 96.0, 104.1)
b. Confirm (using R) that both of your vectors have the same number of
individuals in them.
length(oxygen_consumption_feeding)
## [1] 10
length(oxygen_consumption_non_feeding)
## [1] 10
## [1] 31.78
f. Sometimes ratios are easier to analyze when we look at the log of the
ratio. Create a vector which gives the log of the ratios from the
previous step. (Use the natural log.) What is the mean of this log-ratio?
log_oxygen_consumption <- log(ratio_oxygen_consumption)
log_oxygen_consumption
a. Use read.csv() to read the data from this file into a dataframe
called countries. I can’t figure this one out.
setwd("~/Desktop/Bio 341")
countries <- read.csv("countries.csv", stringsAsFactors = TRUE)
## country total_population_in_thousands_2015
## Afghanistan : 1 Min. : 1.6
## Albania : 1 1st Qu.: 1875.8
## Algeria : 1 Median : 8069.6
## Andorra : 1 Mean : 37721.9
## Angola : 1 3rd Qu.: 26413.0
## Antigua and Barbuda: 1 Max. :1400000.0
## (Other) :190 NA's :2
## gross_national_income_per_capita_2013
life_expectancy_at_birth_female
## Min. : 600 Min. :48.80
## life_expectancy_at_birth_male
life_expectancy_at_age_60_female
## Min. :47.40 Min. :12.70
## life_expectancy_at_age_60_male physicians_density_per_1000
## Min. :12.50 Min. :0.029
## 1st Qu.:15.80 1st Qu.:1.681
## Median :17.50 Median :2.765
## Mean :18.07 Mean :2.725
## 3rd Qu.:20.20 3rd Qu.:3.510
## Max. :23.90 Max. :7.519
## NA's :13 NA's :125
## number_neonatal_deaths_in_thousands_2014
measles_immunization_oneyearolds
## Min. : 0.00 Min. :22.00
## NA's :2 NA's :2
## dpt2_vaccination_oneyearolds
fines_for_tobacco_advertising_2014
## Min. :20.00 No : 54
## 1st Qu.:84.25 Yes :140
## Mean :87.91
## 3rd Qu.:97.00
## Max. :99.00
## NA's :2
## mortality_rate_cancer_2012 cigarette_price_2014
continent
## Min. : 54.00 Min. : 0.360
Africa :54
## 1st Qu.: 88.62 1st Qu.: 1.320
Asia :44
## Median :108.00 Median : 2.620
Europe :47
## Mean :109.64 Mean : 3.798 North
America:23
## 3rd Qu.:124.53 3rd Qu.: 4.965
Oceania :16
## Max. :223.00 Max. :16.140 South
America:12
## NA's :24 NA's :89
## ecological_footprint_2000 ecological_footprint_2012
## Min. : 0.600 Min. :0.700
## 1st Qu.: 1.097 1st Qu.:1.400
## Median : 2.140 Median :2.000
## Mean : 3.147 Mean :2.353
## 3rd Qu.: 4.872 3rd Qu.:3.000
## Max. :15.990 Max. :5.300
## NA's :58 NA's :147
## cell_phone_subscriptions_per_100_people_2012
## Min. : 5.47
## 1st Qu.: 69.83
## Median :103.25
## Mean : 99.90
## 3rd Qu.:126.10
## Max. :198.62
## NA's :10
e. CAN’T DO
f. Add a new column to your countries data frame that has the
difference in ecological footprint between 2012 and 2000. What
is the mean of this difference? (Note: this variable will have
“missing data”, which means that some of the countries do not
have data in this file for one or the other of the years of
ecological footprint. By default, R doesn’t calculate a mean
unless all the data are present. To tell R to ignore the missing
data, add an option to the mean() command that says
na.rm=TRUE. We’ll learn more about this later.)
g. CAN’T DO
4. Using the countries data again, create a new data frame called
AfricaData, that only includes data for countries in Africa. What is the
sum of the total_population_in_thousands_2015 for this new data
frame?
5. CAN’T DO