Introduction to R - Cedarville University

advertisement
Introduction to R
Steven Gollmer
Cedarville University
Downloading R
• Where
– http://r-project.org
– CRAN (Chose download site)
– Linux, MacOS X, Windows
• Packages
– Download *.zip files
– Install packages (local/CRAN)
R Gui
• R Gui
–
–
–
–
R can be run through an
online server using R
Studio and other
corporate software.
Console
Script
Graphics
Help (R Manuals)
• R Studio
–
–
–
–
Files
Workspace
History
Help
Basic Syntax
• Based on Scheme
[1] Indicates which element in the list begins
the line. If multiple lines are displayed, [n] will
precede the nth element (the first element on
that line).
– A dialect of Lisp
– LISt Processing
• Main data structure – linked lists
• Assign data to a list
# Assign a list
a <- c(1 , 2, “bug”, TRUE)
b <- c(1:8)
d <- array( 1:20, dim=c(4,5))
a[3]
d[3,4]
d[3,]
d[,3]
a
“1” “2” “bug” “TRUE”
b
[1] 1 2 3 4 5 6 7 8
d
[,1] [,2] [,3] [,4] [,5]
[1,]
1 5 9 13 17
[2,]
2 6 10 14 18
[3,]
3 7 11 15 19
[4,]
4 8 12 16 20
a[3]
[1] “bug”
d[3,4]
[1] 15
d[3,]
[1] 3 7 11 15 19
d[,3]
[1] 9 10 11 12
Common Tasks
• Comments (#)
• Assign values
– assign( “x”, c(1, 2, 3, 4, 5))
– x <- c(1, 2, 3, 4, 5)
• Generate sequential values
–
–
–
–
–
x <- 1:100
1, 2, 3, 4, …
x <- c(1:100)
same
x <- (1:100)/2
0.5, 1.0, 1.5, 2.0, …
x <- seq(0.5, 50, by=.5)
same
x <- seq( length=100, from=0.5, by=.5)
same
More Tasks
• Conditionals
– if(), else
• Loops
– for()
– while()
– break
if(a<b) {
a <- c
c <- d
} else {
c <- a
d <- c
}
for(j in 1:length(a)) {
plot( a$x[[j]], a$y[[j]] )
}
while(a<b) {
a <- a+1
}
Importing Data
• Comma Separated Values
– a <- data.frame( read.csv(“filename”))
• White space separated values with a
header
– a <- read.table( “filename”, header=TRUE)
• Access data from data frame
– a$freq
• Expose data frame variables
– attach( a )
– freq
– detach( a )
freq
nocore withcore
9.77 0.03984 0.02885
13.02 0.05296 0.03648
16.28 0.05876 0.04289
19.53 0.06609 0.05418
26.04 0.07616 0.06517
29.3 0.07982 0.07799
39.06 0.09293
0.1152
52.08
0.1015
0.1543
61.85
0.1042
0.1812
74.87
0.1082
0.2117
91.15
0.111
0.2077
113.9
0.1138
0.1784
146.5
0.1165
0.1354
179
0.1171
0.114
227.9
0.118 0.08806
273.4
0.1158
0.0789
345.1
0.1107 0.05998
446
0.1021 0.04869
# Monte Carlo simulation of the value of Pi
# Set up the number of Monte Carlo simulations to run
nexp <- 10000
plotflag <- TRUE
# Set the RNG seed each time through the simulation
rngseed <- 2937
set.seed( rngseed, kind = "default", normal.kind = "default" )
success <- 0
# Generate multiple points
x <- runif( nexp, min=0, max=1 )
y <- runif( nexp, min=0, max=1 )
# Set the boundary for the circle (No square root taken because the radius is 1)
radius <- x^2 + y^2
# Plot the points if chosen
if( plotflag ) {
plot(x,y, type="p", pch=".", col="red", main="Estimate of Pi")
step <- c(1:100)/100
xcircle <- cos(pi*step/2)
ycircle <- sin(pi*step/2)
lines( xcircle, ycircle, col="blue", lwd="4" )
}
# Count number that are inside the circle (radius <= 1)
for( i in 1:nexp ) {
if( radius[i] <= 1.0 ) {
success <- success + 1
}
}
# Calculate estimate of pi as % of points within the radius times 4 (4 quadrants)
p <- success/nexp
piresults <- p*4
# Calculate the absolute error and generate a 95% confidence interval
errorestimate <- sqrt((1-p)/success)
pistd <- errorestimate*piresults
pi95 <- 2*pistd
sprintf( "Ave = %f ± %f", piresults, pi95 )
Resources
• An Introduction to R
– http://cran.r-project.org/doc/manuals/r-release/R-intro.html
• R FAQ
– http://cran.r-project.org/bin/windows/base/rw-FAQ.html
• Other Documentation
– http://www.r-project.org/other-docs.html
• The R Journal
– http://journal.r-project.org/current.html
• R Wiki
– http://rwiki.sciviews.org/doku.php
• R Gallery
– http://gallery.r-enthusiasts.com/
Credits
• www.r-project.org – R statistics program
• http://en.wikipedia.org – Wikipedia (Images)
Download