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

Slides:



Advertisements
Similar presentations
Web 2.0 Programming 1 © Tongji University, Computer Science and Technology. Web Web Programming Technology 2012.
Advertisements

Chapter 13: Query Processing
© 2002 D & D Enterprises 1 Linking Images For Navigation & Clickable Image Maps.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 2 Getting Started.
BASIC SKILLS AND TOOLS USING ACCESS
Introduction to Metview
XP New Perspectives on Microsoft Office Word 2003 Tutorial 6 1 Microsoft Office Word 2003 Tutorial 6 – Creating Form Letters and Mailing Labels.
Introduction to Matlab
1 SESSION 5 Graphs for data analysis. 2 Objectives To be able to use STATA to produce exploratory and presentation graphs In particular Bar Charts Histograms.
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Managing Your Site – Lesson 61 Managing Your Site Lesson 6.
1.
Chapter 7: Arrays In this chapter, you will learn about
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
HTML Tags and Their Functions
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Data Structures Using C++
Arrays Dr. Jey Veerasamy July 31 st – August 23 rd 9:30 am to 12 noon 1.
School of Geography FACULTY OF ENVIRONMENT Working with Tables 1.
1 Web-Enabled Decision Support Systems Access Introduction: Touring Access Prof. Name Position (123) University Name.
Creating Tables in a Web Site
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Chapter 5 Microsoft Excel 2007 Window
A very short introduction to R Pia Wohland. R is… -A statistical software -Programming language -Free! -Very good in handling and manipulating data sets.
1 Research Methods Lecture 2 The dummies’ guide to STATA Wiji Arulampalam 18/10/2006.
R for Macroecology Aarhus University, Spring 2011.
How to create a wiki using pbwiki. Step 1: Choose a wiki website I have chosen.
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Enhancing Spotfire with the Power of R
1 BRState Software Demonstration. 2 After you click on the LDEQ link to download the BRState Software you will get this message.
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
Macromedia Dreamweaver MX 2004 – Design Professional Dreamweaver GETTING STARTED WITH.
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Chapter 12 Working with Forms Principles of Web Design, 4 th Edition.
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Computer Concepts BASICS 4th Edition
Created Revised Office of Information, Technology and Accountability 1 Microsoft Access Combos & Command Boxes.
Lesson 16 Enhancing Documents
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Chapter 8 Improving the User Interface
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Basics of R, Ch  Functions  Help  Managing your Objects  Getting Data into R  Getting Results out of R 1 © Fall 2004 Don Edwards and the University.
Basics of Using R Xiao He 1. AGENDA 1.What is R? 2.Basic operations 3.Different types of data objects 4.Importing data 5.Basic data manipulation 2.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
How to Use the R Programming Language for Statistical Analyses Part I: An Introduction to R Jennifer Urbano Blackford, Ph.D. Department of Psychiatry Kennedy.
Introduction to R Statistical Software Anthony (Tony) R. Olsen USEPA ORD NHEERL Western Ecology Division Corvallis, OR (541)
Basic R Programming for Life Science Undergraduate Students Introductory Workshop (Session 1) 1.
An introduction to R: get familiar with R Guangxu Liu Bio7932.
732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.
Arko Barman with modification by C.F. Eick COSC 4335 Data Mining Spring 2015.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Piotr Wolski Introduction to R. Topics What is R? Sample session How to install R? Minimum you have to know to work in R Data objects in R and how to.
Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,
R Programming Yang, Yufei. Normal distribution.
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training.
STAT 534: Statistical Computing Hari Narayanan
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Chris Knight Beginners’ workshop.
Review > x[-c(1,4,6)] > Y[1:3,2:8] > island.data fishData$weight[1] > fishData[fishData$weight < 20 & fishData$condition.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Working with data in R 2 Fish 552: Lecture 3. Recommended Reading An Introduction to R (R Development Core Team) –
Introduction to R Samal Dharmarathna.
Introduction to R Studio
Lab 1 Introductions to R Sean Potter.
Code is on the Website Outline Comparison of Excel and R
Basics of R, Ch Functions Help Managing your Objects
Presentation transcript:

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

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.

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:

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

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)

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

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

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(,, …)

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

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)

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

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

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]

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

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

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,]

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,]

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,]

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

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

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

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

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

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