Mike McCann
22-23 January 2015
Four sections in RStudio:
Console: Where commands are run
Scripts: Where commands are written & saved
Environments: Where objects are stored
Plots & Help: View data, help
Type directly into console or type into script then run (CTRL + ENTER)
A script is a plain text file with R commands in it.
Ends in the file extension .R or .txt
R has many mathematical operators
+ Addition - Subtraction * Multiplication / Division ^ Exponentiation %% Modulus (finds remainder) %/% Integer division (leaves off remainder)R has many logical operators
< Less than<= Less than or equal to > Greater than>= Greater than or equal to== Exactly equal to != not equal to ! NOT| OR& ANDWhat is 17 multiplied by 365?
What is 7 cubed?
Is 9 to the fourth equal to the sum of 2000 and 187 multiplied by 3?
An object is the fundamental unit in R.
All expressions can be saved as an object.
To create an object from an expression we use the assignment operater <-
<- assigns values on the right to objects on the left.
a <- 12+180
a
[1] 192
The object a is now the output of the expression 12+180.
Check your Environment (upper right panel)
Use
#signs to comment on your script.Anything to the right of a
#is ignored.Good scripts (and homework) have comments before every major block of code.
5 + 5 # this adds five and five
[1] 10
# 10 + 10 this does not add ten and ten
The beauty of R is that objects can be combined into other, larger, and more complex objects.
a <- 8*10
b <- 2*10
d <- a*b
d
[1] 1600
# This is equivalent to:
d <- 8*10*2*10
d
[1] 1600
R has ~5 common data structures.
We will start with the simplest: vectors.
Vectors are one dimensional strings of numbers, character, or objects.
A vector is made using the function c().
a <- c(3,4,5)
a
[1] 3 4 5
b <- c("q","r","s")
b
[1] "q" "r" "s"
# make some objects
a <- 4*7
b <- 6*5
g <- 9*2
# combine 'em
d <- c(a,b,g)
d
[1] 28 30 18
You can access any position of the vector using the square brackets [ ]
# make a vector
d <- c(72,6)
d
[1] 72 6
d[1] # get the 1st object in the vector
[1] 72
d[2] # get the 2nd
[1] 6
What is the 9th and 12th position of seq(1,27,0.5)?
Bonus! Can you find those positions simultaneously?
Bonus! Make a vector “Green”, “Eggs”. How would you add Ham to that vector?
A function is a saved object that performs a task given some inputs.
All functions are made up of smaller objects.
Functions are used in the format:
name_of_function(inputs)
The output of a function can be saved to an object:
output <- name_of_function(inputs)
You have already used the funcion c().
It combines the arguments you give it into a vector.
The arguments are separated by commas.
c(3,4,5)
[1] 3 4 5
You have already also seen the funcion seq().
It generates regular sequences given three arguments.
seq(from=1, to=10, by=1)
[1] 1 2 3 4 5 6 7 8 9 10
Not necessary to explicitly name arguments (but it is often helpful).
seq(1,10,1)
[1] 1 2 3 4 5 6 7 8 9 10
Use sum() to take the sum a vector:
sum(c(3,4,5))
[1] 12
Use mean() to take the mean of a vector:
mean(c(3,4,5))
[1] 4
Functions can act on an object
# assign the numbers to a vector
a <- c(3,4,5)
# use the vector as the input to the function
mean(a)
[1] 4
All functions come with a help screen.
It is critical that you learn to read the help screens, since they provide important information on what the function does, how it works, and ususally provide examples at the very bottom.
help(mean)
Even quicker…
Use ? before a function name to view the help screen
?mean # Same as help(mean) ?sort # Same as help(sort)
Some R functions are easy to guess their name
Most functions are abbreviated to save time and space.
Use ?? to search for functions
e.g., Search for any function whose help screens contain the word “robust”
??robustNote: This will only work for installed packages
What is the median of 34, 16, 105, 27?
Remember: functions are often named intuitively.
What does the function range() do, what is the sample example in the help file?
Bonus! Is mean(4,5) different than mean(c(4,5))?
We will be exploring functions in much greater detail throughout this course.
Functions are the soul of R, that is why we use it.
Functions are kept inside packages, some of which come pre-installed with R.
Others must be downloaded.
There are tons of R packages!
List of R Packages and search with your favorite keyword!
Ecology, paleo, dispersal, population, time series, phylogenetic, community, Bayes
Often you will need to install a package to access a certain library of functions.
# Install a new package
install.packages("picante")
# If RStudo asks, pick a closeby mirror and say "okay"" if it asks to create a new folder
Remember to surround the package name in quotation marks.
Installing a package just downloads its to your computer.
To actually use a function from an outside package you have to “load it.”
This let's R know what packages to load in, and not waste time with all potential functions.
# Two ways to load packages:
library(ggplot2)
require(ggplot2)
Note: no quotation marks needed
Good scripts (and homeworks) have a series of require() or library() statements at the top of the script.
Search & find a fascinating package. What is it? What is one interesting function?
Install the package to your computer.
ls() will list all of the obects in your environment.
rm() will remove individual objects
x <- 10
rm(x)
rm(list=ls()) will remove everthing in your workspace
a <- 1; b <- 2; d <- 3; e <- 4
rm(list=ls())
Always remember to save scripts.
For now we will not save workspaces.
Type in quit() or q() and answer N to quit.