100 likes | 251 Views
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.
E N D
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 can be run through an online server using R Studio and other corporate software. • R Gui • Console • Script • Graphics • Help (R Manuals) • R Studio • Files • Workspace • History • Help
[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). Basic Syntax 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 • Based on Scheme • A dialect of Lisp • LIStProcessing • 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]
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 if(a<b) { a <- c c <- d } else { c <- a d <- c } • Conditionals • if(), else • Loops • for() • while() • break 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 )
# 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)