SlideShare a Scribd company logo
1 of 71
Download to read offline
Lab 1 – Introduction to R
August 13 & 14, 2018
FANR 6750
Richard Chandler and Bob Cooper
University of Georgia
Today’s Topics
1 Why Use R?
2 Installing R
3 Basic Usage
Basic calculations
Vectors
Data frames
Importing and Exporting Data
Removing objects and saving workspaces
4 Getting help
Today’s Topics
1 Why Use R?
2 Installing R
3 Basic Usage
Basic calculations
Vectors
Data frames
Importing and Exporting Data
Removing objects and saving workspaces
4 Getting help
Good and Not So Good Things About R
Good
• Powerful platform for statistical analysis
• Many packages written for ecologists
• It’s free
• Scripts save time
• R teaches you statistics
Why Use R? Installing R Basic Usage Getting help 3 / 30
Good and Not So Good Things About R
Good
• Powerful platform for statistical analysis
• Many packages written for ecologists
• It’s free
• Scripts save time
• R teaches you statistics
Not so good??
• Steep learning curve
• Help pages written for people familiar with R
• Developed by statisticians for statisticians
• Not as fast as some languages
Why Use R? Installing R Basic Usage Getting help 3 / 30
Downloading R
• Go to
www.r-project.org
• Click on “CRAN”
• Choose a mirror near
you (there is one in TN)
Why Use R? Installing R Basic Usage Getting help 4 / 30
Using the R GUI
R’s “Graphical User Interface” is operating system specific. Here is
how it looks under Windows.
Why Use R? Installing R Basic Usage Getting help 5 / 30
Alternatives to the R Gui
Emacs and ESS
http://vgoulet.act.ulaval.ca/en/emacs/
RStudio
http://www.rstudio.com/
You are encouraged to learn and use these programs, but we will not use
them for instruction because we want to focus on R itself, not the
interface.
Why Use R? Installing R Basic Usage Getting help 6 / 30
How to read the code in the lab slides
Anything in a shaded box like this one is R code:
2+2
## [1] 4
Why Use R? Installing R Basic Usage Getting help 7 / 30
How to read the code in the lab slides
Anything in a shaded box like this one is R code:
2+2
## [1] 4
The line 2+2 is R code (input). You can copy and paste it into
the console. Note that the command prompt (>) is not shown.
The line ## [1] 4 is output. Output is always indicated by two
hash signs (##). Anything after a # is ignored by R at the
command line.
Why Use R? Installing R Basic Usage Getting help 7 / 30
How to read the code in the lab slides
Anything in a shaded box like this one is R code:
2+2
## [1] 4
The line 2+2 is R code (input). You can copy and paste it into
the console. Note that the command prompt (>) is not shown.
The line ## [1] 4 is output. Output is always indicated by two
hash signs (##). Anything after a # is ignored by R at the
command line.
You can copy and paste the entire code box directly into your
console, but it might be easier to work with the R script that
accompanies the PDF: lab-intro-to-R.R.
Why Use R? Installing R Basic Usage Getting help 7 / 30
Overgrown calculator
Square-root of 3
sqrt(3)
## [1] 1.732051
Why Use R? Installing R Basic Usage Getting help 8 / 30
Overgrown calculator
Square-root of 3
sqrt(3)
## [1] 1.732051
6 squared
6^2
## [1] 36
Why Use R? Installing R Basic Usage Getting help 8 / 30
Overgrown calculator
Square-root of 3
sqrt(3)
## [1] 1.732051
6 squared
6^2
## [1] 36
cosine of π
cos(pi)
## [1] -1
Why Use R? Installing R Basic Usage Getting help 8 / 30
Objects and assignment arrow
Everything in R is an object. We can create objects using the <-
assignment arrow.
Why Use R? Installing R Basic Usage Getting help 9 / 30
Objects and assignment arrow
Everything in R is an object. We can create objects using the <-
assignment arrow.
In this example, we assign the value 2 to the object y :
y <- 2
Why Use R? Installing R Basic Usage Getting help 9 / 30
Objects and assignment arrow
Everything in R is an object. We can create objects using the <-
assignment arrow.
In this example, we assign the value 2 to the object y :
y <- 2
You cannot have a space between < and -
Why Use R? Installing R Basic Usage Getting help 9 / 30
Objects and assignment arrow
Everything in R is an object. We can create objects using the <-
assignment arrow.
In this example, we assign the value 2 to the object y :
y <- 2
You cannot have a space between < and -
You can use = instead of <- but this can cause confusion
Why Use R? Installing R Basic Usage Getting help 9 / 30
Objects and assignment arrow
Everything in R is an object. We can create objects using the <-
assignment arrow.
In this example, we assign the value 2 to the object y :
y <- 2
You cannot have a space between < and -
You can use = instead of <- but this can cause confusion
Typing the name of an object returns its value:
y
## [1] 2
Why Use R? Installing R Basic Usage Getting help 9 / 30
Vectors
We store data in objects so that they can be easily manipulated:
y*2+1
## [1] 5
Why Use R? Installing R Basic Usage Getting help 10 / 30
Vectors
We store data in objects so that they can be easily manipulated:
y*2+1
## [1] 5
Usually, we want more than one number in an object. In statistics,
a vector is simply a set of numbers that can be thought of as a row
or column of a matrix
Why Use R? Installing R Basic Usage Getting help 10 / 30
Vectors
We store data in objects so that they can be easily manipulated:
y*2+1
## [1] 5
Usually, we want more than one number in an object. In statistics,
a vector is simply a set of numbers that can be thought of as a row
or column of a matrix
The easiest way to create a vector is to use the c function to
“combine” numbers:
z <- c(-1, 9, 33, -4)
z
## [1] -1 9 33 -4
Why Use R? Installing R Basic Usage Getting help 10 / 30
Other useful ways of creating vectors
A sequence of numbers
x1 <- 1:3 # Same as: x1 <- c(1, 2, 3)
# Note: anything after "#" is a comment
x1
## [1] 1 2 3
Why Use R? Installing R Basic Usage Getting help 11 / 30
Other useful ways of creating vectors
A sequence of numbers
x1 <- 1:3 # Same as: x1 <- c(1, 2, 3)
# Note: anything after "#" is a comment
x1
## [1] 1 2 3
seq is more general
x2 <- seq(from=1, to=7, by=2)
x2
## [1] 1 3 5 7
Why Use R? Installing R Basic Usage Getting help 11 / 30
Other useful ways of creating vectors
A sequence of numbers
x1 <- 1:3 # Same as: x1 <- c(1, 2, 3)
# Note: anything after "#" is a comment
x1
## [1] 1 2 3
seq is more general
x2 <- seq(from=1, to=7, by=2)
x2
## [1] 1 3 5 7
Use rep to repeat elements of a vector
rep(x2, times=2)
## [1] 1 3 5 7 1 3 5 7
Why Use R? Installing R Basic Usage Getting help 11 / 30
Help with a function
These do the same thing
?rep
help(rep)
Why Use R? Installing R Basic Usage Getting help 12 / 30
Types of vectors
Numeric vectors are used for continuous variables
y1 <- c(2.1, 3.5, 99.0)
class(y1)
## [1] "numeric"
Why Use R? Installing R Basic Usage Getting help 13 / 30
Types of vectors
Numeric vectors are used for continuous variables
y1 <- c(2.1, 3.5, 99.0)
class(y1)
## [1] "numeric"
Factors can be used to store categorical variables:
y2 <- factor(c("Treatment", "Control", "Treatment"))
y2
## [1] Treatment Control Treatment
## Levels: Control Treatment
Why Use R? Installing R Basic Usage Getting help 13 / 30
Vectorized arithmetic
How could we calculate the body mass index (BMI =
weight/height2
) from the following data:
Individual
1 2 3 4 5 6
Weight 60 72 57 90 95 72
Height 1.8 1.8 1.7 1.9 1.7 1.9
Why Use R? Installing R Basic Usage Getting help 14 / 30
Vectorized arithmetic
How could we calculate the body mass index (BMI =
weight/height2
) from the following data:
Individual
1 2 3 4 5 6
Weight 60 72 57 90 95 72
Height 1.8 1.8 1.7 1.9 1.7 1.9
First, create the vectors:
weight <- c(60, 72, 57, 90, 95, 72)
height <- c(1.8, 1.8, 1.7, 1.9, 1.7, 1.9)
Why Use R? Installing R Basic Usage Getting help 14 / 30
Vectorized arithmetic
How could we calculate the body mass index (BMI =
weight/height2
) from the following data:
Individual
1 2 3 4 5 6
Weight 60 72 57 90 95 72
Height 1.8 1.8 1.7 1.9 1.7 1.9
First, create the vectors:
weight <- c(60, 72, 57, 90, 95, 72)
height <- c(1.8, 1.8, 1.7, 1.9, 1.7, 1.9)
Then, evaluate the equation in just one line:
BMI <- weight/height^2
BMI
## [1] 18.51852 22.22222 19.72318 24.93075 32.87197 19.94460
Why Use R? Installing R Basic Usage Getting help 14 / 30
In-class assignment
Calculate the circumference and area of circles with radii: 3,5,6,11
Why Use R? Installing R Basic Usage Getting help 15 / 30
In-class assignment
Calculate the circumference and area of circles with radii: 3,5,6,11
(1) Create a new script called “lab1-prob1.R”. You can do this by:
i Clicking on the Console
ii Choosing "File > New Script" from the drop-down menu
iii Clicking on "File > Save as..."
(2) Create a vector containing the radii
(3) Store the computed circumferences and areas in 2 new vectors
You should be able to do this in just 3 lines in your script.
Write all of your code in the script, not in console.
Why Use R? Installing R Basic Usage Getting help 15 / 30
A common beginner’s problem
If you accidentally hit “return” or fail to complete a command, you
will see the cursor on a new line beginning with + instead of >.
Why Use R? Installing R Basic Usage Getting help 16 / 30
A common beginner’s problem
If you accidentally hit “return” or fail to complete a command, you
will see the cursor on a new line beginning with + instead of >.
Why Use R? Installing R Basic Usage Getting help 16 / 30
A common beginner’s problem
If you accidentally hit “return” or fail to complete a command, you
will see the cursor on a new line beginning with + instead of >.
Just hit the “Esc” key or complete the command.
Why Use R? Installing R Basic Usage Getting help 16 / 30
Summarizing vectors
Number of ticks on 5 dogs
y1 y2 y3 y4 y5
4 7 2 3 150
What is the sum of this vector?
5
i=1 yi = ???
Why Use R? Installing R Basic Usage Getting help 17 / 30
Summarizing vectors
Number of ticks on 5 dogs
y1 y2 y3 y4 y5
4 7 2 3 150
What is the sum of this vector?
5
i=1 yi = ???
y <- c(4,7,2,3,150)
sum(y)
## [1] 166
Why Use R? Installing R Basic Usage Getting help 17 / 30
Summarizing vectors
Number of ticks on 5 dogs
y1 y2 y3 y4 y5
4 7 2 3 150
What is the sum of this vector?
5
i=1 yi = ???
y <- c(4,7,2,3,150)
sum(y)
## [1] 166
What is the mean?
5
i=1 yi
5 = ???
Why Use R? Installing R Basic Usage Getting help 17 / 30
Summarizing vectors
Number of ticks on 5 dogs
y1 y2 y3 y4 y5
4 7 2 3 150
What is the sum of this vector?
5
i=1 yi = ???
y <- c(4,7,2,3,150)
sum(y)
## [1] 166
What is the mean?
5
i=1 yi
5 = ???
mean(y)
## [1] 33.2
Why Use R? Installing R Basic Usage Getting help 17 / 30
Summarizing vectors
Number of ticks on 5 dogs
y1 y2 y3 y4 y5
4 7 2 3 150
What is the sum of this vector?
5
i=1 yi = ???
y <- c(4,7,2,3,150)
sum(y)
## [1] 166
What is the mean?
5
i=1 yi
5 = ???
mean(y)
## [1] 33.2
And the variance?
5
i=1(yi−¯y)2
5−1 = ???
var(y)
## [1] 4266.7
Why Use R? Installing R Basic Usage Getting help 17 / 30
Indexing vectors
Extract the first and third elements of a vector
y <- c(2, 4, 8, 4, 25)
y.sub1 <- y[c(1,3)]
y.sub1
## [1] 2 8
Why Use R? Installing R Basic Usage Getting help 18 / 30
Indexing vectors
Extract the first and third elements of a vector
y <- c(2, 4, 8, 4, 25)
y.sub1 <- y[c(1,3)]
y.sub1
## [1] 2 8
Remove the second element
y.sub2 <- y[-2]
y.sub2
## [1] 2 8 4 25
Why Use R? Installing R Basic Usage Getting help 18 / 30
Indexing vectors
Extract the first and third elements of a vector
y <- c(2, 4, 8, 4, 25)
y.sub1 <- y[c(1,3)]
y.sub1
## [1] 2 8
Remove the second element
y.sub2 <- y[-2]
y.sub2
## [1] 2 8 4 25
Rearrange the order of the vector
y.re <- y[c(5,4,3,2,1)]
y.re
## [1] 25 4 8 4 2
Why Use R? Installing R Basic Usage Getting help 18 / 30
Indexing vectors
Which elements of the vector are greater than 4? (logical test)
y <- c(2, 4, 6, 4, 25)
y>4
## [1] FALSE FALSE TRUE FALSE TRUE
Why Use R? Installing R Basic Usage Getting help 19 / 30
Indexing vectors
Which elements of the vector are greater than 4? (logical test)
y <- c(2, 4, 6, 4, 25)
y>4
## [1] FALSE FALSE TRUE FALSE TRUE
Extract the elements greater than 4 (logical indexing)
y.sub4 <- y[y>4]
y.sub4
## [1] 6 25
Why Use R? Installing R Basic Usage Getting help 19 / 30
Data frames
Most basic datasets are stored as data.frames
Why Use R? Installing R Basic Usage Getting help 20 / 30
Data frames
Most basic datasets are stored as data.frames
They are like a matrix in which each column can be a different
type of vector (numeric, factor, etc...)
Why Use R? Installing R Basic Usage Getting help 20 / 30
Data frames
Most basic datasets are stored as data.frames
They are like a matrix in which each column can be a different
type of vector (numeric, factor, etc...)
They have attributes for row names (e.g. the names of the
experimental units) and column names (e.g. the names of the
response and predictor variables)
Why Use R? Installing R Basic Usage Getting help 20 / 30
Creating a data frame
Simple example with 3 variables measured at 4 sites
y <- c(3, 9, 7, 4)
x1 <- factor(c('High', 'High', 'Low', 'Low'))
x2 <- c(2.2, 3.4, 4.4, 3.9)
mydata <- data.frame(Goats=y, Elev=x1, Temp=x2)
rownames(mydata) <- c('Site1', 'Site2', 'Site3', 'Site4')
mydata
## Goats Elev Temp
## Site1 3 High 2.2
## Site2 9 High 3.4
## Site3 7 Low 4.4
## Site4 4 Low 3.9
Why Use R? Installing R Basic Usage Getting help 21 / 30
Indexing data frames
Bracket method. Extract data from row 1, columns 1 and 3
mydata[1,c(1,3)]
## Goats Temp
## Site1 3 2.2
Why Use R? Installing R Basic Usage Getting help 22 / 30
Indexing data frames
Bracket method. Extract data from row 1, columns 1 and 3
mydata[1,c(1,3)]
## Goats Temp
## Site1 3 2.2
Bracket method. Extract from rows 2 and 3, columns 1 and 3
mydata[c('Site2', 'Site3'), c('Goats', 'Temp')]
## Goats Temp
## Site2 9 3.4
## Site3 7 4.4
Why Use R? Installing R Basic Usage Getting help 22 / 30
Indexing data frames
Bracket method. Extract data from row 1, columns 1 and 3
mydata[1,c(1,3)]
## Goats Temp
## Site1 3 2.2
Bracket method. Extract from rows 2 and 3, columns 1 and 3
mydata[c('Site2', 'Site3'), c('Goats', 'Temp')]
## Goats Temp
## Site2 9 3.4
## Site3 7 4.4
Dollar sign method. Pull out column 1
mydata$Elev
## [1] High High Low Low
## Levels: High Low
Why Use R? Installing R Basic Usage Getting help 22 / 30
Summarizing data frames
View the data as a ‘spreadsheet’
View(mydata)
Why Use R? Installing R Basic Usage Getting help 23 / 30
Summarizing data frames
View the data as a ‘spreadsheet’
View(mydata)
Compute some summary statistics
summary(mydata)
## Goats Elev Temp
## Min. :3.00 High:2 Min. :2.200
## 1st Qu.:3.75 Low :2 1st Qu.:3.100
## Median :5.50 Median :3.650
## Mean :5.75 Mean :3.475
## 3rd Qu.:7.50 3rd Qu.:4.025
## Max. :9.00 Max. :4.400
Why Use R? Installing R Basic Usage Getting help 23 / 30
Summarizing data frames
View the data as a ‘spreadsheet’
View(mydata)
Compute some summary statistics
summary(mydata)
## Goats Elev Temp
## Min. :3.00 High:2 Min. :2.200
## 1st Qu.:3.75 Low :2 1st Qu.:3.100
## Median :5.50 Median :3.650
## Mean :5.75 Mean :3.475
## 3rd Qu.:7.50 3rd Qu.:4.025
## Max. :9.00 Max. :4.400
A very compact summary
str(mydata)
## 'data.frame': 4 obs. of 3 variables:
## $ Goats: num 3 9 7 4
## $ Elev : Factor w/ 2 levels "High","Low": 1 1 2 2
## $ Temp : num 2.2 3.4 4.4 3.9
Why Use R? Installing R Basic Usage Getting help 23 / 30
Importing and exporting data
read.csv and write.csv are easy options, but there are many
more possibilities that we won’t cover.
Why Use R? Installing R Basic Usage Getting help 24 / 30
Importing and exporting data
read.csv and write.csv are easy options, but there are many
more possibilities that we won’t cover.
Export the data.frame we created earlier
write.csv(mydata, file="mydata.csv")
Why Use R? Installing R Basic Usage Getting help 24 / 30
Importing and exporting data
read.csv and write.csv are easy options, but there are many
more possibilities that we won’t cover.
Export the data.frame we created earlier
write.csv(mydata, file="mydata.csv")
Confirm that it is there:
getwd() # Go to this location and look for 'mydata.csv'
Why Use R? Installing R Basic Usage Getting help 24 / 30
Importing and exporting data
read.csv and write.csv are easy options, but there are many
more possibilities that we won’t cover.
Export the data.frame we created earlier
write.csv(mydata, file="mydata.csv")
Confirm that it is there:
getwd() # Go to this location and look for 'mydata.csv'
Read it back in:
mydata2 <- read.csv("mydata.csv")
Why Use R? Installing R Basic Usage Getting help 24 / 30
The working directory
The working directory is the location on your computer where
R will look for files by default.
Why Use R? Installing R Basic Usage Getting help 25 / 30
The working directory
The working directory is the location on your computer where
R will look for files by default.
You can check your working directory like this:
getwd()
## [1] "d:/exp-design/labs/intro-to-R"
Why Use R? Installing R Basic Usage Getting help 25 / 30
The working directory
The working directory is the location on your computer where
R will look for files by default.
You can check your working directory like this:
getwd()
## [1] "d:/exp-design/labs/intro-to-R"
Change your working directory to another location:
## Note the forward slashes, which could be replaced by ""
setwd("C:/work/courses/")
Why Use R? Installing R Basic Usage Getting help 25 / 30
The working directory
The working directory is the location on your computer where
R will look for files by default.
You can check your working directory like this:
getwd()
## [1] "d:/exp-design/labs/intro-to-R"
Change your working directory to another location:
## Note the forward slashes, which could be replaced by ""
setwd("C:/work/courses/")
At the beginning of every R session, you should use setwd
to set your working directory
Why Use R? Installing R Basic Usage Getting help 25 / 30
The workspace
Viewing the objects in your workspace
ls()
## [1] "BMI" "clean" "filestub" "height" "mydata" "mydata2"
## [7] "open" "reqval" "rnw.file" "tangle" "weight" "x1"
## [13] "x2" "y" "y.re" "y.sub1" "y.sub2" "y.sub4"
## [19] "y1" "y2" "z"
Why Use R? Installing R Basic Usage Getting help 26 / 30
The workspace
Viewing the objects in your workspace
ls()
## [1] "BMI" "clean" "filestub" "height" "mydata" "mydata2"
## [7] "open" "reqval" "rnw.file" "tangle" "weight" "x1"
## [13] "x2" "y" "y.re" "y.sub1" "y.sub2" "y.sub4"
## [19] "y1" "y2" "z"
Removing (deleting) some objects
rm(x1, x2, mydata2, y, y1, y2, y.re,
y.sub1, y.sub2, y.sub4, height, weight, BMI, z)
ls()
## [1] "clean" "filestub" "mydata" "open" "reqval" "rnw.file"
## [7] "tangle"
Why Use R? Installing R Basic Usage Getting help 26 / 30
Saving and restoring the workspace
Why Use R? Installing R Basic Usage Getting help 27 / 30
Saving and restoring the workspace
If you prefer commands over point-and-click:
save.image("myimage.RData") # Save all objects to file
load("myimage.RData") # Load the saved workspace
Why Use R? Installing R Basic Usage Getting help 27 / 30
Additional Resources
‘Official’ manuals
help.start()
Useful books
• Venables, W.N. and B.D. Ripley. 2002. Modern Applied
Statistics with S, 4th ed. Springer.
• Crawley, M.J.. 2013. The R Book, 2nd ed. Wiley
Online
• Always Google your error messages
• http://stackoverflow.com/
• https://preludeinr.com/
Why Use R? Installing R Basic Usage Getting help 28 / 30
Assignment – Part I
(1) Create an R script named something like Chandler-R_lab1.R
(2) In your R script, write code to create a data.frame that contains the
information shown in the table below
(3) Add code to export the data.frame as a .csv file and then import it back
into R.
Individual Mass Weight Treatment
1 3 4 Control
2 3 5 Control
3 2 4 Control
4 4 6 Treatment
5 5 5 Treatment
6 5 7 Treatment
Upload your R script1
to ELC before your next lab. The script should be
self-contained, meaning that it will run correctly when we copy and paste it in
the console.
1
If you are familiar with RMarkdown, you can submit a .Rmd file instead of a .R file
Why Use R? Installing R Basic Usage Getting help 29 / 30
Assignment – Part II
Read chapters 1, 4, & 5 before next lab
Dalgaard, P. 2008. Introductory Statistics with R. 2nd edition. Springer.
Available for free through the UGA library:
http://preproxy.galib.uga.edu/login?url=http://dx.doi.org/10.1007/978-0-387-79054-1
Why Use R? Installing R Basic Usage Getting help 30 / 30

More Related Content

What's hot

Piecewise functions
Piecewise functionsPiecewise functions
Piecewise functionsLori Rapp
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureSriram Raj
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in MatlabJorge Jasso
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming languageMegha V
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queueskalyanineve
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iteratePhilip Schwarz
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3Philip Schwarz
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programmingVisnuDharsini
 

What's hot (20)

Piecewise functions
Piecewise functionsPiecewise functions
Piecewise functions
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Arrays
ArraysArrays
Arrays
 
Gr10 step function ppt
Gr10 step function pptGr10 step function ppt
Gr10 step function ppt
 
Array and functions
Array and functionsArray and functions
Array and functions
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
 
Data Structure
Data StructureData Structure
Data Structure
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
 
Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 

Similar to Introduction to R - Lab slides for UGA course FANR 6750

R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfKabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfattalurilalitha
 
5. R basics
5. R basics5. R basics
5. R basicsFAO
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptanshikagoel52
 
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfINTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfranapoonam1
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdfBusyBird2
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxSONU KUMAR
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordLakshmi Sarvani Videla
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Spencer Fox
 
2015-10-23_wim_davis_r_slides.pptx on consumer
2015-10-23_wim_davis_r_slides.pptx on consumer2015-10-23_wim_davis_r_slides.pptx on consumer
2015-10-23_wim_davis_r_slides.pptx on consumertirlukachaitanya
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxshandicollingwood
 

Similar to Introduction to R - Lab slides for UGA course FANR 6750 (20)

R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
5. R basics
5. R basics5. R basics
5. R basics
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
R basics
R basicsR basics
R basics
 
Rbootcamp Day 1
Rbootcamp Day 1Rbootcamp Day 1
Rbootcamp Day 1
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfINTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016
 
2015-10-23_wim_davis_r_slides.pptx on consumer
2015-10-23_wim_davis_r_slides.pptx on consumer2015-10-23_wim_davis_r_slides.pptx on consumer
2015-10-23_wim_davis_r_slides.pptx on consumer
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
 

More from richardchandler

Model Selection and Multi-model Inference
Model Selection and Multi-model InferenceModel Selection and Multi-model Inference
Model Selection and Multi-model Inferencerichardchandler
 
Introduction to Generalized Linear Models
Introduction to Generalized Linear ModelsIntroduction to Generalized Linear Models
Introduction to Generalized Linear Modelsrichardchandler
 
Introduction to statistical modeling in R
Introduction to statistical modeling in RIntroduction to statistical modeling in R
Introduction to statistical modeling in Rrichardchandler
 
Repeated measures analysis in R
Repeated measures analysis in RRepeated measures analysis in R
Repeated measures analysis in Rrichardchandler
 
Lab on contrasts, estimation, and power
Lab on contrasts, estimation, and powerLab on contrasts, estimation, and power
Lab on contrasts, estimation, and powerrichardchandler
 
t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750richardchandler
 
Hierarchichal species distributions model and Maxent
Hierarchichal species distributions model and MaxentHierarchichal species distributions model and Maxent
Hierarchichal species distributions model and Maxentrichardchandler
 
The role of spatial models in applied ecological research
The role of spatial models in applied ecological researchThe role of spatial models in applied ecological research
The role of spatial models in applied ecological researchrichardchandler
 

More from richardchandler (17)

Model Selection and Multi-model Inference
Model Selection and Multi-model InferenceModel Selection and Multi-model Inference
Model Selection and Multi-model Inference
 
Introduction to Generalized Linear Models
Introduction to Generalized Linear ModelsIntroduction to Generalized Linear Models
Introduction to Generalized Linear Models
 
Introduction to statistical modeling in R
Introduction to statistical modeling in RIntroduction to statistical modeling in R
Introduction to statistical modeling in R
 
ANCOVA in R
ANCOVA in RANCOVA in R
ANCOVA in R
 
Repeated measures analysis in R
Repeated measures analysis in RRepeated measures analysis in R
Repeated measures analysis in R
 
Split-plot Designs
Split-plot DesignsSplit-plot Designs
Split-plot Designs
 
Nested Designs
Nested DesignsNested Designs
Nested Designs
 
Factorial designs
Factorial designsFactorial designs
Factorial designs
 
Blocking lab
Blocking labBlocking lab
Blocking lab
 
Assumptions of ANOVA
Assumptions of ANOVAAssumptions of ANOVA
Assumptions of ANOVA
 
Lab on contrasts, estimation, and power
Lab on contrasts, estimation, and powerLab on contrasts, estimation, and power
Lab on contrasts, estimation, and power
 
One-way ANOVA
One-way ANOVAOne-way ANOVA
One-way ANOVA
 
t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750
 
Hierarchichal species distributions model and Maxent
Hierarchichal species distributions model and MaxentHierarchichal species distributions model and Maxent
Hierarchichal species distributions model and Maxent
 
Slides from ESA 2015
Slides from ESA 2015Slides from ESA 2015
Slides from ESA 2015
 
The role of spatial models in applied ecological research
The role of spatial models in applied ecological researchThe role of spatial models in applied ecological research
The role of spatial models in applied ecological research
 
2014 ISEC slides
2014 ISEC slides2014 ISEC slides
2014 ISEC slides
 

Recently uploaded

Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsssuserddc89b
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physicsvishikhakeshava1
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 

Recently uploaded (20)

Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physics
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physics
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 

Introduction to R - Lab slides for UGA course FANR 6750

  • 1. Lab 1 – Introduction to R August 13 & 14, 2018 FANR 6750 Richard Chandler and Bob Cooper University of Georgia
  • 2. Today’s Topics 1 Why Use R? 2 Installing R 3 Basic Usage Basic calculations Vectors Data frames Importing and Exporting Data Removing objects and saving workspaces 4 Getting help
  • 3. Today’s Topics 1 Why Use R? 2 Installing R 3 Basic Usage Basic calculations Vectors Data frames Importing and Exporting Data Removing objects and saving workspaces 4 Getting help
  • 4. Good and Not So Good Things About R Good • Powerful platform for statistical analysis • Many packages written for ecologists • It’s free • Scripts save time • R teaches you statistics Why Use R? Installing R Basic Usage Getting help 3 / 30
  • 5. Good and Not So Good Things About R Good • Powerful platform for statistical analysis • Many packages written for ecologists • It’s free • Scripts save time • R teaches you statistics Not so good?? • Steep learning curve • Help pages written for people familiar with R • Developed by statisticians for statisticians • Not as fast as some languages Why Use R? Installing R Basic Usage Getting help 3 / 30
  • 6. Downloading R • Go to www.r-project.org • Click on “CRAN” • Choose a mirror near you (there is one in TN) Why Use R? Installing R Basic Usage Getting help 4 / 30
  • 7. Using the R GUI R’s “Graphical User Interface” is operating system specific. Here is how it looks under Windows. Why Use R? Installing R Basic Usage Getting help 5 / 30
  • 8. Alternatives to the R Gui Emacs and ESS http://vgoulet.act.ulaval.ca/en/emacs/ RStudio http://www.rstudio.com/ You are encouraged to learn and use these programs, but we will not use them for instruction because we want to focus on R itself, not the interface. Why Use R? Installing R Basic Usage Getting help 6 / 30
  • 9. How to read the code in the lab slides Anything in a shaded box like this one is R code: 2+2 ## [1] 4 Why Use R? Installing R Basic Usage Getting help 7 / 30
  • 10. How to read the code in the lab slides Anything in a shaded box like this one is R code: 2+2 ## [1] 4 The line 2+2 is R code (input). You can copy and paste it into the console. Note that the command prompt (>) is not shown. The line ## [1] 4 is output. Output is always indicated by two hash signs (##). Anything after a # is ignored by R at the command line. Why Use R? Installing R Basic Usage Getting help 7 / 30
  • 11. How to read the code in the lab slides Anything in a shaded box like this one is R code: 2+2 ## [1] 4 The line 2+2 is R code (input). You can copy and paste it into the console. Note that the command prompt (>) is not shown. The line ## [1] 4 is output. Output is always indicated by two hash signs (##). Anything after a # is ignored by R at the command line. You can copy and paste the entire code box directly into your console, but it might be easier to work with the R script that accompanies the PDF: lab-intro-to-R.R. Why Use R? Installing R Basic Usage Getting help 7 / 30
  • 12. Overgrown calculator Square-root of 3 sqrt(3) ## [1] 1.732051 Why Use R? Installing R Basic Usage Getting help 8 / 30
  • 13. Overgrown calculator Square-root of 3 sqrt(3) ## [1] 1.732051 6 squared 6^2 ## [1] 36 Why Use R? Installing R Basic Usage Getting help 8 / 30
  • 14. Overgrown calculator Square-root of 3 sqrt(3) ## [1] 1.732051 6 squared 6^2 ## [1] 36 cosine of π cos(pi) ## [1] -1 Why Use R? Installing R Basic Usage Getting help 8 / 30
  • 15. Objects and assignment arrow Everything in R is an object. We can create objects using the <- assignment arrow. Why Use R? Installing R Basic Usage Getting help 9 / 30
  • 16. Objects and assignment arrow Everything in R is an object. We can create objects using the <- assignment arrow. In this example, we assign the value 2 to the object y : y <- 2 Why Use R? Installing R Basic Usage Getting help 9 / 30
  • 17. Objects and assignment arrow Everything in R is an object. We can create objects using the <- assignment arrow. In this example, we assign the value 2 to the object y : y <- 2 You cannot have a space between < and - Why Use R? Installing R Basic Usage Getting help 9 / 30
  • 18. Objects and assignment arrow Everything in R is an object. We can create objects using the <- assignment arrow. In this example, we assign the value 2 to the object y : y <- 2 You cannot have a space between < and - You can use = instead of <- but this can cause confusion Why Use R? Installing R Basic Usage Getting help 9 / 30
  • 19. Objects and assignment arrow Everything in R is an object. We can create objects using the <- assignment arrow. In this example, we assign the value 2 to the object y : y <- 2 You cannot have a space between < and - You can use = instead of <- but this can cause confusion Typing the name of an object returns its value: y ## [1] 2 Why Use R? Installing R Basic Usage Getting help 9 / 30
  • 20. Vectors We store data in objects so that they can be easily manipulated: y*2+1 ## [1] 5 Why Use R? Installing R Basic Usage Getting help 10 / 30
  • 21. Vectors We store data in objects so that they can be easily manipulated: y*2+1 ## [1] 5 Usually, we want more than one number in an object. In statistics, a vector is simply a set of numbers that can be thought of as a row or column of a matrix Why Use R? Installing R Basic Usage Getting help 10 / 30
  • 22. Vectors We store data in objects so that they can be easily manipulated: y*2+1 ## [1] 5 Usually, we want more than one number in an object. In statistics, a vector is simply a set of numbers that can be thought of as a row or column of a matrix The easiest way to create a vector is to use the c function to “combine” numbers: z <- c(-1, 9, 33, -4) z ## [1] -1 9 33 -4 Why Use R? Installing R Basic Usage Getting help 10 / 30
  • 23. Other useful ways of creating vectors A sequence of numbers x1 <- 1:3 # Same as: x1 <- c(1, 2, 3) # Note: anything after "#" is a comment x1 ## [1] 1 2 3 Why Use R? Installing R Basic Usage Getting help 11 / 30
  • 24. Other useful ways of creating vectors A sequence of numbers x1 <- 1:3 # Same as: x1 <- c(1, 2, 3) # Note: anything after "#" is a comment x1 ## [1] 1 2 3 seq is more general x2 <- seq(from=1, to=7, by=2) x2 ## [1] 1 3 5 7 Why Use R? Installing R Basic Usage Getting help 11 / 30
  • 25. Other useful ways of creating vectors A sequence of numbers x1 <- 1:3 # Same as: x1 <- c(1, 2, 3) # Note: anything after "#" is a comment x1 ## [1] 1 2 3 seq is more general x2 <- seq(from=1, to=7, by=2) x2 ## [1] 1 3 5 7 Use rep to repeat elements of a vector rep(x2, times=2) ## [1] 1 3 5 7 1 3 5 7 Why Use R? Installing R Basic Usage Getting help 11 / 30
  • 26. Help with a function These do the same thing ?rep help(rep) Why Use R? Installing R Basic Usage Getting help 12 / 30
  • 27. Types of vectors Numeric vectors are used for continuous variables y1 <- c(2.1, 3.5, 99.0) class(y1) ## [1] "numeric" Why Use R? Installing R Basic Usage Getting help 13 / 30
  • 28. Types of vectors Numeric vectors are used for continuous variables y1 <- c(2.1, 3.5, 99.0) class(y1) ## [1] "numeric" Factors can be used to store categorical variables: y2 <- factor(c("Treatment", "Control", "Treatment")) y2 ## [1] Treatment Control Treatment ## Levels: Control Treatment Why Use R? Installing R Basic Usage Getting help 13 / 30
  • 29. Vectorized arithmetic How could we calculate the body mass index (BMI = weight/height2 ) from the following data: Individual 1 2 3 4 5 6 Weight 60 72 57 90 95 72 Height 1.8 1.8 1.7 1.9 1.7 1.9 Why Use R? Installing R Basic Usage Getting help 14 / 30
  • 30. Vectorized arithmetic How could we calculate the body mass index (BMI = weight/height2 ) from the following data: Individual 1 2 3 4 5 6 Weight 60 72 57 90 95 72 Height 1.8 1.8 1.7 1.9 1.7 1.9 First, create the vectors: weight <- c(60, 72, 57, 90, 95, 72) height <- c(1.8, 1.8, 1.7, 1.9, 1.7, 1.9) Why Use R? Installing R Basic Usage Getting help 14 / 30
  • 31. Vectorized arithmetic How could we calculate the body mass index (BMI = weight/height2 ) from the following data: Individual 1 2 3 4 5 6 Weight 60 72 57 90 95 72 Height 1.8 1.8 1.7 1.9 1.7 1.9 First, create the vectors: weight <- c(60, 72, 57, 90, 95, 72) height <- c(1.8, 1.8, 1.7, 1.9, 1.7, 1.9) Then, evaluate the equation in just one line: BMI <- weight/height^2 BMI ## [1] 18.51852 22.22222 19.72318 24.93075 32.87197 19.94460 Why Use R? Installing R Basic Usage Getting help 14 / 30
  • 32. In-class assignment Calculate the circumference and area of circles with radii: 3,5,6,11 Why Use R? Installing R Basic Usage Getting help 15 / 30
  • 33. In-class assignment Calculate the circumference and area of circles with radii: 3,5,6,11 (1) Create a new script called “lab1-prob1.R”. You can do this by: i Clicking on the Console ii Choosing "File > New Script" from the drop-down menu iii Clicking on "File > Save as..." (2) Create a vector containing the radii (3) Store the computed circumferences and areas in 2 new vectors You should be able to do this in just 3 lines in your script. Write all of your code in the script, not in console. Why Use R? Installing R Basic Usage Getting help 15 / 30
  • 34. A common beginner’s problem If you accidentally hit “return” or fail to complete a command, you will see the cursor on a new line beginning with + instead of >. Why Use R? Installing R Basic Usage Getting help 16 / 30
  • 35. A common beginner’s problem If you accidentally hit “return” or fail to complete a command, you will see the cursor on a new line beginning with + instead of >. Why Use R? Installing R Basic Usage Getting help 16 / 30
  • 36. A common beginner’s problem If you accidentally hit “return” or fail to complete a command, you will see the cursor on a new line beginning with + instead of >. Just hit the “Esc” key or complete the command. Why Use R? Installing R Basic Usage Getting help 16 / 30
  • 37. Summarizing vectors Number of ticks on 5 dogs y1 y2 y3 y4 y5 4 7 2 3 150 What is the sum of this vector? 5 i=1 yi = ??? Why Use R? Installing R Basic Usage Getting help 17 / 30
  • 38. Summarizing vectors Number of ticks on 5 dogs y1 y2 y3 y4 y5 4 7 2 3 150 What is the sum of this vector? 5 i=1 yi = ??? y <- c(4,7,2,3,150) sum(y) ## [1] 166 Why Use R? Installing R Basic Usage Getting help 17 / 30
  • 39. Summarizing vectors Number of ticks on 5 dogs y1 y2 y3 y4 y5 4 7 2 3 150 What is the sum of this vector? 5 i=1 yi = ??? y <- c(4,7,2,3,150) sum(y) ## [1] 166 What is the mean? 5 i=1 yi 5 = ??? Why Use R? Installing R Basic Usage Getting help 17 / 30
  • 40. Summarizing vectors Number of ticks on 5 dogs y1 y2 y3 y4 y5 4 7 2 3 150 What is the sum of this vector? 5 i=1 yi = ??? y <- c(4,7,2,3,150) sum(y) ## [1] 166 What is the mean? 5 i=1 yi 5 = ??? mean(y) ## [1] 33.2 Why Use R? Installing R Basic Usage Getting help 17 / 30
  • 41. Summarizing vectors Number of ticks on 5 dogs y1 y2 y3 y4 y5 4 7 2 3 150 What is the sum of this vector? 5 i=1 yi = ??? y <- c(4,7,2,3,150) sum(y) ## [1] 166 What is the mean? 5 i=1 yi 5 = ??? mean(y) ## [1] 33.2 And the variance? 5 i=1(yi−¯y)2 5−1 = ??? var(y) ## [1] 4266.7 Why Use R? Installing R Basic Usage Getting help 17 / 30
  • 42. Indexing vectors Extract the first and third elements of a vector y <- c(2, 4, 8, 4, 25) y.sub1 <- y[c(1,3)] y.sub1 ## [1] 2 8 Why Use R? Installing R Basic Usage Getting help 18 / 30
  • 43. Indexing vectors Extract the first and third elements of a vector y <- c(2, 4, 8, 4, 25) y.sub1 <- y[c(1,3)] y.sub1 ## [1] 2 8 Remove the second element y.sub2 <- y[-2] y.sub2 ## [1] 2 8 4 25 Why Use R? Installing R Basic Usage Getting help 18 / 30
  • 44. Indexing vectors Extract the first and third elements of a vector y <- c(2, 4, 8, 4, 25) y.sub1 <- y[c(1,3)] y.sub1 ## [1] 2 8 Remove the second element y.sub2 <- y[-2] y.sub2 ## [1] 2 8 4 25 Rearrange the order of the vector y.re <- y[c(5,4,3,2,1)] y.re ## [1] 25 4 8 4 2 Why Use R? Installing R Basic Usage Getting help 18 / 30
  • 45. Indexing vectors Which elements of the vector are greater than 4? (logical test) y <- c(2, 4, 6, 4, 25) y>4 ## [1] FALSE FALSE TRUE FALSE TRUE Why Use R? Installing R Basic Usage Getting help 19 / 30
  • 46. Indexing vectors Which elements of the vector are greater than 4? (logical test) y <- c(2, 4, 6, 4, 25) y>4 ## [1] FALSE FALSE TRUE FALSE TRUE Extract the elements greater than 4 (logical indexing) y.sub4 <- y[y>4] y.sub4 ## [1] 6 25 Why Use R? Installing R Basic Usage Getting help 19 / 30
  • 47. Data frames Most basic datasets are stored as data.frames Why Use R? Installing R Basic Usage Getting help 20 / 30
  • 48. Data frames Most basic datasets are stored as data.frames They are like a matrix in which each column can be a different type of vector (numeric, factor, etc...) Why Use R? Installing R Basic Usage Getting help 20 / 30
  • 49. Data frames Most basic datasets are stored as data.frames They are like a matrix in which each column can be a different type of vector (numeric, factor, etc...) They have attributes for row names (e.g. the names of the experimental units) and column names (e.g. the names of the response and predictor variables) Why Use R? Installing R Basic Usage Getting help 20 / 30
  • 50. Creating a data frame Simple example with 3 variables measured at 4 sites y <- c(3, 9, 7, 4) x1 <- factor(c('High', 'High', 'Low', 'Low')) x2 <- c(2.2, 3.4, 4.4, 3.9) mydata <- data.frame(Goats=y, Elev=x1, Temp=x2) rownames(mydata) <- c('Site1', 'Site2', 'Site3', 'Site4') mydata ## Goats Elev Temp ## Site1 3 High 2.2 ## Site2 9 High 3.4 ## Site3 7 Low 4.4 ## Site4 4 Low 3.9 Why Use R? Installing R Basic Usage Getting help 21 / 30
  • 51. Indexing data frames Bracket method. Extract data from row 1, columns 1 and 3 mydata[1,c(1,3)] ## Goats Temp ## Site1 3 2.2 Why Use R? Installing R Basic Usage Getting help 22 / 30
  • 52. Indexing data frames Bracket method. Extract data from row 1, columns 1 and 3 mydata[1,c(1,3)] ## Goats Temp ## Site1 3 2.2 Bracket method. Extract from rows 2 and 3, columns 1 and 3 mydata[c('Site2', 'Site3'), c('Goats', 'Temp')] ## Goats Temp ## Site2 9 3.4 ## Site3 7 4.4 Why Use R? Installing R Basic Usage Getting help 22 / 30
  • 53. Indexing data frames Bracket method. Extract data from row 1, columns 1 and 3 mydata[1,c(1,3)] ## Goats Temp ## Site1 3 2.2 Bracket method. Extract from rows 2 and 3, columns 1 and 3 mydata[c('Site2', 'Site3'), c('Goats', 'Temp')] ## Goats Temp ## Site2 9 3.4 ## Site3 7 4.4 Dollar sign method. Pull out column 1 mydata$Elev ## [1] High High Low Low ## Levels: High Low Why Use R? Installing R Basic Usage Getting help 22 / 30
  • 54. Summarizing data frames View the data as a ‘spreadsheet’ View(mydata) Why Use R? Installing R Basic Usage Getting help 23 / 30
  • 55. Summarizing data frames View the data as a ‘spreadsheet’ View(mydata) Compute some summary statistics summary(mydata) ## Goats Elev Temp ## Min. :3.00 High:2 Min. :2.200 ## 1st Qu.:3.75 Low :2 1st Qu.:3.100 ## Median :5.50 Median :3.650 ## Mean :5.75 Mean :3.475 ## 3rd Qu.:7.50 3rd Qu.:4.025 ## Max. :9.00 Max. :4.400 Why Use R? Installing R Basic Usage Getting help 23 / 30
  • 56. Summarizing data frames View the data as a ‘spreadsheet’ View(mydata) Compute some summary statistics summary(mydata) ## Goats Elev Temp ## Min. :3.00 High:2 Min. :2.200 ## 1st Qu.:3.75 Low :2 1st Qu.:3.100 ## Median :5.50 Median :3.650 ## Mean :5.75 Mean :3.475 ## 3rd Qu.:7.50 3rd Qu.:4.025 ## Max. :9.00 Max. :4.400 A very compact summary str(mydata) ## 'data.frame': 4 obs. of 3 variables: ## $ Goats: num 3 9 7 4 ## $ Elev : Factor w/ 2 levels "High","Low": 1 1 2 2 ## $ Temp : num 2.2 3.4 4.4 3.9 Why Use R? Installing R Basic Usage Getting help 23 / 30
  • 57. Importing and exporting data read.csv and write.csv are easy options, but there are many more possibilities that we won’t cover. Why Use R? Installing R Basic Usage Getting help 24 / 30
  • 58. Importing and exporting data read.csv and write.csv are easy options, but there are many more possibilities that we won’t cover. Export the data.frame we created earlier write.csv(mydata, file="mydata.csv") Why Use R? Installing R Basic Usage Getting help 24 / 30
  • 59. Importing and exporting data read.csv and write.csv are easy options, but there are many more possibilities that we won’t cover. Export the data.frame we created earlier write.csv(mydata, file="mydata.csv") Confirm that it is there: getwd() # Go to this location and look for 'mydata.csv' Why Use R? Installing R Basic Usage Getting help 24 / 30
  • 60. Importing and exporting data read.csv and write.csv are easy options, but there are many more possibilities that we won’t cover. Export the data.frame we created earlier write.csv(mydata, file="mydata.csv") Confirm that it is there: getwd() # Go to this location and look for 'mydata.csv' Read it back in: mydata2 <- read.csv("mydata.csv") Why Use R? Installing R Basic Usage Getting help 24 / 30
  • 61. The working directory The working directory is the location on your computer where R will look for files by default. Why Use R? Installing R Basic Usage Getting help 25 / 30
  • 62. The working directory The working directory is the location on your computer where R will look for files by default. You can check your working directory like this: getwd() ## [1] "d:/exp-design/labs/intro-to-R" Why Use R? Installing R Basic Usage Getting help 25 / 30
  • 63. The working directory The working directory is the location on your computer where R will look for files by default. You can check your working directory like this: getwd() ## [1] "d:/exp-design/labs/intro-to-R" Change your working directory to another location: ## Note the forward slashes, which could be replaced by "" setwd("C:/work/courses/") Why Use R? Installing R Basic Usage Getting help 25 / 30
  • 64. The working directory The working directory is the location on your computer where R will look for files by default. You can check your working directory like this: getwd() ## [1] "d:/exp-design/labs/intro-to-R" Change your working directory to another location: ## Note the forward slashes, which could be replaced by "" setwd("C:/work/courses/") At the beginning of every R session, you should use setwd to set your working directory Why Use R? Installing R Basic Usage Getting help 25 / 30
  • 65. The workspace Viewing the objects in your workspace ls() ## [1] "BMI" "clean" "filestub" "height" "mydata" "mydata2" ## [7] "open" "reqval" "rnw.file" "tangle" "weight" "x1" ## [13] "x2" "y" "y.re" "y.sub1" "y.sub2" "y.sub4" ## [19] "y1" "y2" "z" Why Use R? Installing R Basic Usage Getting help 26 / 30
  • 66. The workspace Viewing the objects in your workspace ls() ## [1] "BMI" "clean" "filestub" "height" "mydata" "mydata2" ## [7] "open" "reqval" "rnw.file" "tangle" "weight" "x1" ## [13] "x2" "y" "y.re" "y.sub1" "y.sub2" "y.sub4" ## [19] "y1" "y2" "z" Removing (deleting) some objects rm(x1, x2, mydata2, y, y1, y2, y.re, y.sub1, y.sub2, y.sub4, height, weight, BMI, z) ls() ## [1] "clean" "filestub" "mydata" "open" "reqval" "rnw.file" ## [7] "tangle" Why Use R? Installing R Basic Usage Getting help 26 / 30
  • 67. Saving and restoring the workspace Why Use R? Installing R Basic Usage Getting help 27 / 30
  • 68. Saving and restoring the workspace If you prefer commands over point-and-click: save.image("myimage.RData") # Save all objects to file load("myimage.RData") # Load the saved workspace Why Use R? Installing R Basic Usage Getting help 27 / 30
  • 69. Additional Resources ‘Official’ manuals help.start() Useful books • Venables, W.N. and B.D. Ripley. 2002. Modern Applied Statistics with S, 4th ed. Springer. • Crawley, M.J.. 2013. The R Book, 2nd ed. Wiley Online • Always Google your error messages • http://stackoverflow.com/ • https://preludeinr.com/ Why Use R? Installing R Basic Usage Getting help 28 / 30
  • 70. Assignment – Part I (1) Create an R script named something like Chandler-R_lab1.R (2) In your R script, write code to create a data.frame that contains the information shown in the table below (3) Add code to export the data.frame as a .csv file and then import it back into R. Individual Mass Weight Treatment 1 3 4 Control 2 3 5 Control 3 2 4 Control 4 4 6 Treatment 5 5 5 Treatment 6 5 7 Treatment Upload your R script1 to ELC before your next lab. The script should be self-contained, meaning that it will run correctly when we copy and paste it in the console. 1 If you are familiar with RMarkdown, you can submit a .Rmd file instead of a .R file Why Use R? Installing R Basic Usage Getting help 29 / 30
  • 71. Assignment – Part II Read chapters 1, 4, & 5 before next lab Dalgaard, P. 2008. Introductory Statistics with R. 2nd edition. Springer. Available for free through the UGA library: http://preproxy.galib.uga.edu/login?url=http://dx.doi.org/10.1007/978-0-387-79054-1 Why Use R? Installing R Basic Usage Getting help 30 / 30