Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to R Steven Gollmer Cedarville University.

Similar presentations


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

1 Introduction to R Steven Gollmer Cedarville University

2 Downloading R Where – – CRAN (Chose download site) – Linux, MacOS X, Windows Packages – Download *.zip files – Install packages (local/CRAN)

3 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.

4 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] d [,1] [,2] [,3] [,4] [,5] [1,] [2,] [3,] [4,] a[3] [1] “bug” d[3,4] [1] 15 d[3,] [1] d[,3] [1] [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).

5 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

6 More Tasks Conditionals – if(), else Loops – for() – while() – break if(a

7 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 )

8 # Monte Carlo simulation of the value of Pi # Set up the number of Monte Carlo simulations to run nexp < plotflag <- TRUE # Set the RNG seed each time through the simulation rngseed < 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 )

9 Resources An Introduction to R – R FAQ – Other Documentation – The R Journal – R Wiki – R Gallery –

10 Credits – R statistics program – Wikipedia (Images)


Download ppt "Introduction to R Steven Gollmer Cedarville University."

Similar presentations


Ads by Google