Download presentation
Presentation is loading. Please wait.
Published byLenard McDonald Modified over 9 years ago
1
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training
2
Ellen Cheng ellenc@uwice.gov.bt 1772-4714
3
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]
4
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)
5
(data from Wikipedia) Why Use R? Compatible with many operating systems
6
(data from Wikipedia) Why Use R? FREE!
7
(data from Wikipedia) Why Use R? Flexible & powerful statistical program
8
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)
9
Besides Statistics, What Can R Do? Publication-quality charts & graphs
13
Besides Statistics, What Can R Do? Publication-quality charts & graphs Dynamic graphing & animations
16
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
17
Migratory path of a turkey vulture in 2009 (red), 2010 (blue), 2011 (green)
18
Abundance data for two different species (blue, red) plotted on Google Earth map
19
LET’S GET STARTED...
20
Installing R www.r-project.org 1
21
2 Choose a CRAN mirror 3 Choose your Operating System 4 Choose ‘base’ installation 5 Download
22
Installing RStudio www.rstudio.com 1
23
2
24
3
25
RStudio Layout CONSOLE for typing code for immediate execution, and seeing output
26
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
27
RStudio Layout - WORKSPACE tab shows active variables & data - HISTORY tab records all the code as you execute them
28
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
29
CREATE FILES & SET YOUR CURRENT WORKING DIRECTORY
30
1 Browse to your working directory
31
2 Set your working directory
32
getwd() to see your current directory
33
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...)
34
TO SET A DEFAULT DIRECTORY... (if you just open RStudio from the application, this is the directory it will use) MAC: RStudio > Preferences WINDOWS: Tools > Global Options
36
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
37
R Help
38
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
39
R Help
40
www.rseek.org
42
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
43
Packages in this list are already installed. Mark boxes to load the ones you want to use. Now unload the package and type library(datasets) in the console
44
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)
45
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()
46
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”)
47
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
48
Your First R Graph Quakes 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?)
49
plot (quakes$mag, quakes$stations) ALTERNATIVE CODE: with (quakes, plot (mag, stations)) How would you use with() to calculate the mean of quakes$mag?
50
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)
51
TODAY'S REVIEW How 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
52
TODAY'S REVIEW How 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.