# Introduction to R Steven Gollmer Cedarville University.

## Presentation on theme: "Introduction to R Steven Gollmer Cedarville University."— Presentation transcript:

Introduction to R Steven Gollmer Cedarville University

R Gui – Console – Script – Graphics – Help (R Manuals) R Studio – Files – Workspace – History – Help R can be run through an online server using R Studio and other corporate software.

Basic Syntax Based on Scheme – 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 [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).

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4194071/slides/slide_6.jpg", "name": "More Tasks Conditionals – if(), else Loops – for() – while() – break if(a

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 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 http://cran.r-project.org/bin/windows/base/rw-FAQ.html Other Documentation – http://www.r-project.org/other-docs.html http://www.r-project.org/other-docs.html The R Journal – http://journal.r-project.org/current.html http://journal.r-project.org/current.html R Wiki – http://rwiki.sciviews.org/doku.php http://rwiki.sciviews.org/doku.php R Gallery – http://gallery.r-enthusiasts.com/ http://gallery.r-enthusiasts.com/

Credits www.r-project.org – R statistics program www.r-project.org http://en.wikipedia.org – Wikipedia (Images) http://en.wikipedia.org