introduction to r introductions what is r? rstudio layout summary statistics your first r graph 17...
TRANSCRIPT
Introduction to R
IntroductionsWhat is R?
RStudio LayoutSummary StatisticsYour First R Graph
17 September 2014 Sherubtse Training
Ellen [email protected]
1772-4714
Your Turn Name & department
Research or activities for which you might be using R in the future (i.e., why are you here?)
[Compile group email list]
What is ?
... an open source programming language for statistical computing and graphics
... with 5895 statistical “add-on” packages contributed by R-users since 1993
... and an extensive online “help” community (internet discussion site)
Windows Mac OS Linux
R YES YES YES FREE 7 13
S-PLUS YES NO YES $2,399/year 6 6
Minitab YES NO NO $1,395 6 5
SPSS YES YES YES $2,250/year 6 7
SAS YES NO YES $6,000 7 13
Operating SystemCost (USD)
# of ANOVA Types Formally Supported
PROGRAM# of Regression Types Formally Supported
(data from Wikipedia)
Why Use R?
Compatible with many operating systems
Windows Mac OS Linux
R YES YES YES FREE 7 13
S-PLUS YES NO YES $2,399/year 6 6
Minitab YES NO NO $1,395 6 5
SPSS YES YES YES $2,250/year 6 7
SAS YES NO YES $6,000 7 13
Operating SystemCost (USD)
# of ANOVA Types Formally Supported
PROGRAM# of Regression Types Formally Supported
(data from Wikipedia)
Why Use R?
FREE!
Windows Mac OS Linux
R YES YES YES FREE 7 13
S-PLUS YES NO YES $2,399/year 6 6
Minitab YES NO NO $1,395 6 5
SPSS YES YES YES $2,250/year 6 7
SAS YES NO YES $6,000 7 13
Operating SystemCost (USD)
# of ANOVA Types Formally Supported
PROGRAM# of Regression Types Formally Supported
(data from Wikipedia)
Why Use R?
Flexible & powerful statistical program
Why Use R?“R has really become the second language for people coming out of grad school now, and there’s an amazing amount of code being written for it” (Max Kuhn, Pfizer statistician)
“The popularity of R at universities could threaten SAS Institute, the privately held business software company that specializes in data analysis software” (NY Times article, 6-Jan 2009)
“Companies as diverse as Google, Pfizer, Merck, Bank of America, the InterContinental Hotels Group and Shell use [R]...Companies like Google and Pfizer say they use the [R] software for just about anything they can” (NY Times article, 6-Jan 2009)
Besides Statistics, What Can R Do?
• Publication-quality charts & graphs
Besides Statistics, What Can R Do?
• Publication-quality charts & graphs
• Dynamic graphing & animations
Besides Statistics, What Can R Do?
• Publication-quality charts & graphs
• Dynamic graphing & animations
• Link with many other applications, e.g., Google Earth, GIS, etc.
... and many, MANY other things
Migratory path of a turkey vulture in 2009 (red), 2010 (blue), 2011 (green)
Abundance data for two different species (blue, red) plotted on Google Earth map
LET’S GET STARTED...
Installing Rwww.r-project.org
1
2 Choose a CRAN mirror
3
Choose your Operating System
4 Choose ‘base’ installation
5 Download
Installing RStudiowww.rstudio.com
1
2
3
RStudio Layout
CONSOLE for typing code for immediate execution,
and seeing output
RStudio Layout
SOURCE PANE for writing and editing code (script) for later
execution later (like a notepad), and a good way to store notes for this training
RStudio Layout
- WORKSPACE tab shows active variables & data
- HISTORY tab records all the code as you execute them
RStudio Layout- FILES tab shows files & folders in your workspace
- PLOTS tab for viewing graphs
- PACKAGES tab for installing & updating R packages
- HELP tab for R help
CREATE FILES & SET YOUR CURRENT WORKING DIRECTORY
1 Browse to your working directory
2 Set your working directory
getwd() to see your current directory
WHAT IS A WORKING DIRECTORY?1) Create a test script with variables
– check global environment & history
2) Save the test script– where does it save?
3) Close RStudio, then open it from the script– what is the working directory?
4) Close RStudio, then open it from the application– what is the working directory?
NOTE: Can also save workspace with a specific name (Session > Save Workspace As...)
TO SET A DEFAULT DIRECTORY...(if you just open RStudio from the application,
this is the directory it will use)
MAC: RStudio > PreferencesWINDOWS: Tools > Global Options
RStudio Layout- FILES tab shows files & folders in your workspace
- PLOTS tab for viewing graphs
- PACKAGES tab for installing & updating R packages
- HELP tab for R help
R Help
R Help
help.start() to access some R resources off-line
help(topic_name) OR ?topic_name if the relevant package is loaded (use quotes for words with spaces)
help.search(topic_name) OR ??topic_name if the package is not loaded
R Help
www.rseek.org
RStudio Layout- FILES tab shows files & folders in your workspace
- PLOTS tab for viewing graphs
- PACKAGES tab for installing & updating R packages
- HELP tab for R help
Packages in this list are already installed. Mark boxes to load the ones you want to use.
Now unload the package and typelibrary(datasets) in the console
Quakes Dataset
• Read the documentation for this dataset– What do these data describe?– How many data records are there?– What are the 5 variables included in this dataset?
• Type the dataset name in the console
• Now try these functions (use your Source pane!): names(quakes)head(quakes)tail(quakes)
Quakes Dataset• Examine how the history pane relates to the console,
and use it to search & run previous commands (or up-arrow). How would you send commands to a script?
• Learn what these functions do: summary()ncol()nrow()dim()View()
Quakes Dataset• Examine a single column of data: quakes$mag
• Calculate the mean and standard deviation of quake magnitudes
??”standard deviation”help.search(“standard deviation”)
Missing Values• Missing values in your data should be entered as
NA (not available)—not as blanks
• For some functions, you need to specify in function arguments what to do with missing values in the data (e.g., na.rm=TRUE)
• Use the function is.na() to determine if there are missing values in a data set
• What does sum(is.na()) tell us?
Create the vector: x <- c(5, 10, 15, NA, 25)...then calculate the mean of the vector
Your First R GraphQuakes Dataset Is there a relationship between the magnitude of an earthquake and the number of stations reporting?
What kind of graph would we plot? How would we expect the graph to look?
Use plot() to create this graph (which package does this function belong to?)
plot (quakes$mag, quakes$stations)
ALTERNATIVE CODE:with (quakes, plot (mag, stations))
How would you use with() to calculate the mean of quakes$mag?
Learn to Use Function Arguments
Play around with sending arguments to the plot() function. For now, try:
main="Fiji earthquakes since 1964" xlab="Magnitude"ylab="# of Stations Reporting"col="green"
Then export the graph as a jpeg file & save it (easiest to resize before you export)
TODAY'S REVIEWHow would you...• list the first few records of a dataset• see a dataset in Excel format• find out the number of rows & columns in a
dataset• calculate the standard deviation of a vector• get summary information on a dataset • find out if there are missing values in a dataset
TODAY'S REVIEWHow would you...• find out the current working directory• create a simple scatterplot• list the column names for a dataset• load a package• find out the number of rows in a dataset• calculate the mean of a vector