Download presentation

Presentation is loading. Please wait.

Published byBryan Butterfield Modified over 2 years ago

1
Introduction to R:Joseph Powell Overall Aims Introduce programming concepts relevant to MX Demonstrate the strengths (and weaknesses) of R

2
Introduction to R:Joseph Powell Books The R Book – Crawley (2007) Introductions to statistics using R –Cohen Y. and Cohen J. Y. (2008). Statistics and Data with R. –Crawley M. (2005). Statistics: An Introduction using R. –Dalgaard P. (2002). Introductory Statistics with R. –Maindonald J. & Braun J. (2003). Data Analysis and Graphics Using R: An Example-based Approach. Books on biological topics –Paradis E. (2006). Analysis of Phylogenetics and Evolution with R. –Broman K. W. & Sen S. (2009). A Guide to QTL Mapping with R/qtl. –Bolker B.M. (2008). Ecological Models and Data in R. Books on statistical topics –Aitkin M. et al. (2009). Statistical Modelling in R. –Faraway J. (2009). Linear Models with R. –Albert J. (2009). Bayesian Computation with R. –Bivand R.S. et al. (2009). Applied Spatial Data Analysis with R. –Cowpertwait P.S.P. & Metcalfe A.V. (2009). Introductory Time Series with R. Books on R specifics and R programming –Spector P. (2008). Data Manipulation with R. –Murrell P. (2006). R Graphics. –Chambers J. M. (2008). Software for Data Analysis: Programming with R.

3
Introduction to R:Joseph Powell Websites Websites: –Cran R: –R cookbook: –R graphics: –R wiki: –Mailing lists: –R seek: Websites on statistical topics –R genetics: –Bioconductor:

4
Introduction to R: Joseph Powell The console Load up R Console window appears, with a command prompt Everything in the R console can be partitioned into two fundamental operations: –Input variables > x <- 2 –Output variables > x [1] 2

5
Introduction to R: Joseph Powell Objects Names –Case sensitive, no spaces –Must begin with a letter but also can contain numbers and:. _ –Try to give your objects meaningful names > My_f4vourite.langua6e_evR <- “R” x, y and My_f4v… are objects that we have created > ls()# this will bring up a list of all our objects > rm(y)# this deletes y (forever) > rm(list=ls())# this deletes everything (..forever)

6
Introduction to R:Joseph Powell Workspace 1 Everything shown in this list of objects comprises our 'workspace' > ls() [1] "My_f4vourite.langua6e_evR" "x" "y“ > save.image(file=“myworkspace.RData”) > rm(list=ls()) > ls() character(0) > load(file = “myworkspace.RData”) > ls() [1] "My_f4vourite.langua6e_evR" "x" "y“ Objects are internal to R –Does not behave like a file structure on the computer –Can't be read or interpreted outside R (?)

7
Introduction to R:Joseph Powell Workspace 2 You can select which objects to save > save(y, x, file = “two_objects.RData”) Different computer folders can be accessed > dir()# shows current work directory > setwd(“~/work_directory”)# sets R's focus to a different computer folder

8
Introduction to R:Joseph Powell Built-in functions Native functions make R succinct Diverse range available from graphics to data manipulation to statistical algorithms etc. Highly optimised so use them if they are available instead of writing your own Function structure: > function_name(,, …)

9
Introduction to R:Joseph Powell Missing values NA is a “reserved” word in R It is a single element (length 1) that indicates a missing value A helpful alternative to coding missing values (e.g -99) > my_array <- c(NA,100,120,120,120,130,NA) > sum(my_array) [1] NA > sum(my_array,na.rm=T)# most functions allow you to explicitly state how to handle NA [1] 590 > table(my_array)# HOWEVER the default action varies from function to function my_array

10
Introduction to R:Joseph Powell R help pages Each function has its own unique syntax –Default arguments –Data structure requirements –Output options > ?seq# brings up help page of seq() function > ??”sequence”# searches for all related functions Note > seq(from = 2, to = 100, by = 2) is clearer than > seq(2,100,2)

11
Introduction to R:Joseph Powell Basic Scripting Note pad / text editor –Within the R GUI –Open with: File > New Scriptor Ctrl+N –Layout as tile is useful: Windows > Tile

12
Introduction to R:Joseph Powell Basic Scripting Note pad / text editor –Useful for keeping all work together –Scripts can be saved –Can be used to save a “program” –Add # comments –Check individual bits of code –Ctrl+R Whole line Selected code

13
Introduction to R:Joseph Powell Basic Scripting Brackets –( )functions –[ ]subsets –{ }processes Subsets –Take a subset of an object –Objects have either 1 x n, or m x n dimensions > x [,1] [,2] [,3] [,4] [1,] [2,] [3,] > x [1] > x[5] [1] 6 > X[3,4] [1] 12 [rows, columns]

14
Introduction to R:Joseph Powell Basic Scripting Data input –Direct input into the console scan() –Reading in data read.table / read.csv –“name.txt” –“c:\\temp\\name.txt” –choose.file() –list.files() –dir() > y <- scan() 1: 3 2: 4 3: 12 4: 3 5: 5 6: 2 7: 14 8: Read 7 items > dir() [1] "temp.csv" "temp2.csv" “name.txt” > y <- read.table("name.txt", header=T, sep="\t") >

15
Introduction to R:Joseph Powell Basic Scripting Data output –Direct input into the console sink() –Writing out data write.table / write.csv –“name.txt” –“c:\\temp\\name.txt” sink(“sink_tmp.txt”) i <- 1:10 outer(i, i, "*") sink() > dir() [1] "temp.csv" "temp2.csv" “name.txt” > write.table("name.txt", header=T, sep="\t") >

16
Introduction to R:Joseph Powell Basic Scripting Adding rows and columns –Allows objects to be joined, either to an existing object or to make a new object –cbind() – adds columns together –rbind() – adds rows together > y1 [,1] [,2] [,3] [1,] [2,] [3,] [4,] > y2 [,1] [1,] [2,] [3,] [4,] > y3 <- cbind(y1, y2) > y3 [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] > y3 <- rbind(y1, y2[1:3]) > y3 [,1] [,2] [,3] [1,] [2,] [3,] [4,] [5,]

17
Introduction to R:Joseph Powell Basic Scripting for loops –loop through a set of commands a given number of times –very useful, but are not optimal for memory > dim(y) [1] > for(i in 1:ncol(y)) { y_mean <- mean(y[i, 1:10]) } > y_mean [1] > out <- array(0, c(ncol(y), 1)) > for(i in 1:ncol(y)) { out[i] <- mean(y[i, ]) } > out [,1] [1,] [2,] [3,] [4,] [5,] [6,] [7,] [8,] [9,] [10,]

18
Introduction to R:Joseph Powell Data Manipulation Check data –dim() –mydata[1:10, 1:10] –str() –summary() –head() –tail() –table() –etc… > mydata <- read.table("mydata.txt", header=T, sep="\t") > dim(mydata) [1] > mydata[1:10, 1:10] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] [2,] [3,] [4,] [5,] [6,] [7,] [8,] [9,] [10,]

19
Introduction to R:Joseph Powell Data Manipulation Reordering –If you have a data.frame or matrix (numbers or letters) –Use: order() –index <- order(old[,1], decreasing=T) > dim(lamb) [1] > head(lamb) Field Weight sire dam sex 1 A F 2 A F 3 A M 4 A M 5 A F 6 A F > lamb <- lamb[order(lamb$sex, decreasing=F), ] > head(lamb) Field Weight sire dam sex 1 A F 2 A F 5 A F 6 A F 9 A F 10 A F

20
Introduction to R:Joseph Powell Data Manipulation Reordering –order() > lamb <- lamb[order(lamb$sex, decreasing=F), ] > rows <- order(lamb$sex, decreasing=F) > lamb <- lamb[rows, ] > index <- order(lamb$sex, decreasing=F) > head(index) [1] > lamb <- lamb[index, ] Expanded way

21
Introduction to R:Joseph Powell Data Manipulation Replacing –index –which() > class(lamb) [1] “matrix” > head(lamb) Field Weight sire dam sex 1 A F 2 A F 3 B M > index <- lamb[,1]==“A” > head(index) [1] TRUE TRUE FALSE TRUE FALSE > lamb[index, 1] <- ”C” > head(lamb) Field Weight sire dam sex 1 C F 2 C F 3 B M > index <- which(lamb[,1]=="A") > head(index) > lamb[index, 1] <- ”C” > lamb[which(lamb[,1]==”A”, 1] <- ”C” Put it together

22
Introduction to R:Joseph Powell Data Manipulation Replacing > class(lamb) [1] “matrix” > head(lamb) Field Weight sire dam sex 1 A F 2 A F 3 B M > index <- lamb[,2] <= > table(index) index FALSE TRUE > lamb[index, 2] <- ”NA” > which(lamb[,2] >= 20.0 & lamb[,2] <= 21.0) > which(lamb[,1]==“A” & lamb[,2] >= 20.0 & lamb[,2] <= 21.0) > new_lamb = 20.0 & lamb[,2] <= 21.0), ] > new_lamb Field Weight sire dam sex 214 A F 363 A M 496 A M

23
Graphics with R: Overview 1.Why graphics? 2.Why graphics in R? 3.The R graphics systems (did you really expect just one?) 4.Graphics basics and examples 5.Customisation of a graphic 6.Overview of different systems and packages Introduction to R:Joseph Powell

24
plot(x, y, …) > ?Formaldehyde > head(Formaldehyde) carb optden > plot(Formaldehyde) > ?par Introduction to R:Joseph Powell

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google