Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a...
Transcript of Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a...
![Page 1: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/1.jpg)
Intro to Rstat 480
Heike Hofmann
![Page 2: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/2.jpg)
Outline
• Getting Started & Setup
• R basics (refresher)
• Syntax
• Examining Objects
• Extracting Parts
• Basic Graphics: Scatterplots, Histograms, Boxplots
![Page 3: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/3.jpg)
Install R• On your own machine:
• Go to http://www.r-project.org/
• From CRAN, pick download site (ISU might be good)
• Download from base:
• Download newest R version
• Run the installation script
• Download RStudio from www.rstudio.org
• On a lab machine:
• Start RStudio by double-clicking the icon
![Page 4: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/4.jpg)
Setting up
• Download file 07-r-intro.R
• Open the R script in RStudio
• Edit lines
• Cut and paste lines of code into the R interpreter window
![Page 5: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/5.jpg)
ConsoleFile !
Directory
Working !Environment
Pieces of RStudio
![Page 6: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/6.jpg)
The R language
• Learning a new language: grammar, vocabulary
• Loading, examining, summarizing data
• Creating data
• Getting help
• Miscellaneous useful stuff
![Page 7: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/7.jpg)
Learning a new language is hard!
![Page 8: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/8.jpg)
Learning a language
• Grammar / Syntax
• Vocabulary
• “Thinking in that language”
![Page 9: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/9.jpg)
Grammar
• Basic algebra is the same
• but 2*x not 2x, 2^p instead of 2p
• Applying a function is similar
• Making a variable, use <- instead of =
• Everything in R is a vector
• Index a vector using [ ]
Like mathematics
![Page 10: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/10.jpg)
Examples• x = 2 / 3
• √x
• a = 2(x + 3)2
• y = (1 2 3 5)T
• y1
• ∑y
• 2y
• f(y, 2) = 2y
x <- 2 / 3!
sqrt(x)!
a <- 2 *(x + 3)^2!
y <- c(1, 2, 3, 5)!
y[1]!
sum(y)!
2*y!
f <- function(x, y) return(x*y)!
f(y, 2)
![Page 11: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/11.jpg)
You try
• x = (4 1 3 9)T
• y = (1 2 3 5)T
• d = √∑ (xi - yi)2
• 2(y1 + x3)
![Page 12: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/12.jpg)
Vocabulary
• What verbs (=functions) do you need to know?
• Loading data
• Accessing parts of things
• Statistical summaries
• ...
![Page 13: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/13.jpg)
R Reference Card
• Download the R Reference Card fromhttp://cran.r-project.org/doc/contrib/Short-refcard.pdf
• Open/Print so that you can glance at it while working
![Page 14: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/14.jpg)
Loading data
• Import data with:
• read.csv() for csv files
• (and use file.choose() to help find your file)
• Save from excel as csv files (use Save As)
• Stored in a data.frame
![Page 15: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/15.jpg)
Your turn
• Download FBI data fbi.xls from the website, open in Excel and export as csv file
• Load it into R fbi <- read.csv(file.choose())
• Did the data import work?
• Advanced: Try and break the data import, but adding odd characters to excel (try #, , “, ), read ?read.csv and figure out what’s going on
![Page 16: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/16.jpg)
•x!
•head(x)!
•summary(x)!
•str(x)!
•dim(x)
Examining variables
Try these commands out for the fbi object!
![Page 17: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/17.jpg)
Navigating the R interpreter window
• Up/down arrow keys to retrieve previous lines
• Left/right arrow keys to move cursor along line
• Mouse click to set cursor position
• Delete to remove and re-type parts of command
![Page 18: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/18.jpg)
Getting Help
•?command!
•help(command) !
•help.search(command)
Getting Out
•q()
![Page 19: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/19.jpg)
What do we have?
• A data.frame = a list of variables of the same length (but may be different types)
• Has row and column names
![Page 20: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/20.jpg)
Extracting bits of data.frame
•x$variable!
•x[, "variable"]!
•x[rows, columns]!
•x[1:5, 2:3]!
•x[c(1,5,6), c("State","Year")]!
•x$variable[rows]
![Page 21: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/21.jpg)
Statistical summaries
•mean, median, min, max, range!
•sd, var, cor
![Page 22: Intro to R - hofroe.nethofroe.net/stat480/07-r-intro.pdf · • Download RStudio from ! • On a lab machine:! • Start RStudio by double-clicking the icon. Setting up • Download](https://reader030.fdocuments.net/reader030/viewer/2022040411/5ed9ecba8f8ad53f095e99ef/html5/thumbnails/22.jpg)
Your turn
• Compute correlation between Population and number of burglaries
• Look at first 10 data records
• Compute mean and standard deviation for each variable. Why do you get NAs? (read ?NA)
• Advanced: Read ?mean and ?sd, and fix missing value problem