short introduction to r - cimmytgenomics.cimmyt.org/slu/slides paulino/0. r-intro/presenta.pdf ·...

51
Short Introduction to R Paulino Pérez 1 José Crossa 2 1 ColPos-México 2 CIMMyT-México September, 2014. SLU,Sweden Short Introduction to R 1/51

Upload: others

Post on 02-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Short Introduction to R

Paulino Pérez 1

José Crossa 2

1ColPos-México 2CIMMyT-México

September, 2014.

SLU,Sweden Short Introduction to R 1/51

Page 2: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Contents

1 Introduction

2 Simple objects

3 User defined functions

4 Graphs

5 Importing data

6 Installing packages

7 Questions

SLU,Sweden Short Introduction to R 2/51

Page 3: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction ¿R?

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 3/51

Page 4: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction ¿R?

¿R?

R is a software for statistical analysis and graphics. It was developed by RossIhaka y Robert Gentleman. R was specially designed to perform dataanalysis, but can be used also as a programming language.

R is distributed freely under the GNU license(General Public Licence). Thedevelopment is done by the R Development Team.

R is available as source code and binary files compiled for windows and Mac.

The R language allows the user to use loops to perform complex analysis.

SLU,Sweden Short Introduction to R 4/51

Page 5: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 5/51

Page 6: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

R and other softwares

Why R ?R is a free software, it can be executed in Windows, Linux and Mac.Excellent documentation and graphical capabilities.Most of the programs written in S plus can be run in R.It is powerful and easy to learn.It can be extended through the use of packages.

SLU,Sweden Short Introduction to R 6/51

Page 7: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Disadvantages

Graphical user interface not as good as in other softwares.Lack of commercial support (partially true).

SLU,Sweden Short Introduction to R 7/51

Page 8: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Installation in a Windows environmentGo to http://www.r-project.org and download the windows binary,

Figure 1: R web site.

SLU,Sweden Short Introduction to R 8/51

Page 9: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Continue...

Go to the Downloads section and select CRAN,

Figure 2: CRAN mirrors

SLU,Sweden Short Introduction to R 9/51

Page 10: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Continue...

Select the software for you OS,

Figure 3: Executables for various platforms.

SLU,Sweden Short Introduction to R 10/51

Page 11: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Continue...

Download the base software,

Figure 4: Download R-base.

SLU,Sweden Short Introduction to R 11/51

Page 12: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction R and other sofwares

Continue...

Double click in the installer,

Figure 5: Installing R.

SLU,Sweden Short Introduction to R 12/51

Page 13: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Manuals and help

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 13/51

Page 14: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Manuals and help

Manuals

Once that you install R, you will have access to the following manuals in PDFformat:

An Introduction to RR Reference ManualR Data Import/ExportR Language DefinitionWriting R ExtensionsR InternalsR Installation and Administration

SLU,Sweden Short Introduction to R 14/51

Page 15: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Manuals and help

Continue...

Furthermore:

Contributed Docs(http://cran.r-project.org/other-docs.html).R-help mailing list archives(http://cran.r-project.org/search.html). Mailing list.Reference card. Summary of most useful R commands(http://www.rpad.org/Rpad/Rpad-refcard.pdf)S Programming, W. Venables and B. Ripley. Seehttp://www.stats.ox.ac.uk/pub/MASS3/Sprog.

SLU,Sweden Short Introduction to R 15/51

Page 16: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 16/51

Page 17: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Sample R session

Go to a Start->Programs->R->R-3.x.y, the working environment is as thatshown in the next Figure.

Figure 6: R ready to process commands.

SLU,Sweden Short Introduction to R 17/51

Page 18: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

The symbol > is the command prompt. We can write command there, forexample to show some help about matrices,

?matrix

then Enter

The help system can be accessed through the command line using thefollowing functions:

?texthelp.start()help.search("text to search")apropos("search for some thing similar to...")

SLU,Sweden Short Introduction to R 18/51

Page 19: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Code editors

A set of R commands is usually known as “script". There are several texteditors. The one included by default in Windows installations is not as fancyas others that have syntax highlighting, for example Tinn-R, or R-studio.The standard text editor in R can be accessed from the File menu, File->NewScript

Figure 7: Code editor in R

SLU,Sweden Short Introduction to R 19/51

Page 20: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Example (performing basic calculations):

7+42*3*(1+2)

The commands are written in the text editor, then the commands are selectedand the pop-up menu is activated, one of the entries in the menu has anoption to execute the code. The result will appear in the R console.

Figure 8: Executing R commands from the text editor.

SLU,Sweden Short Introduction to R 20/51

Page 21: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Commands written in the text editor can be saved and restored for editinglater.

There exists a lot of text editors for writing R scripts, for example:

WinEdit (shareware)SciViews (freeware)Tinn-R (freeware)Emacs (free)Rstudio (free)

SLU,Sweden Short Introduction to R 21/51

Page 22: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Introduction Sample R session

Continue...

Figure 9: Rstudio.

SLU,Sweden Short Introduction to R 22/51

Page 23: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects

R works manipulating “objects”. The objects are manipulated usingfunctions and operators.

The most basic objects are:

vectors (type numeric or character)Matrixdata.frameListsFunctions

Some useful functions...

General pourpouse: sqrt(),log(),exp(),sin(),cos(), etc.Related to statistics: mean(), sd(), var(), quantile(), etc.

The assignment operator is = with R>=1.4.0 or <- in any R version.

SLU,Sweden Short Introduction to R 23/51

Page 24: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects

Notes:

R distinguish between upper and lowercase lettersThe symbol "#" is used to comment the codeObject’s names can contain any combination of characters, exceptspaces and special symbols, for example "$","%","#", etc.Missing data can be represented with the special symbol "NA" (NotAvailable), and errors in computations for example dividing by 0 with thespecial symbol "NaN" (Not a Number) or "Inf"

SLU,Sweden Short Introduction to R 24/51

Page 25: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Vectors

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 25/51

Page 26: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Vectors

Vectors

Vectors are created using the functions c(),seq(),:, rep().

Examples:

a=c(1,2,3,4,5)

ab=c("a","b","c")bd=1:10

de=seq(1,10,by=0.5)e f=seq(1,10,length.out=20)fg=rep(10,3)gh=c(e,f)h

SLU,Sweden Short Introduction to R 26/51

Page 27: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Vectors

Vector operations

We can perform most common operations using vectors with the same length.Operations are performed element wise.

Examples:

a=c(1,2,3)b=c(2,3,5)a+b #sum a and ba-b #a-ba*b #element wise producta^b #power function3*a+2*b #product and suma/b #element wise quotienta^2 #takes the square of each element

It is also possible to apply a function to a vector, for example:

exp(a) #Exponential functionlog(a) #logarithm functiond=sqrt(a)+log(b) #square root and logarithmd #shows d

SLU,Sweden Short Introduction to R 27/51

Page 28: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Vectors

To extract some elements from the vectors we can use the [] operator, forexample:

a=c(1,2,5,7,9)b=a[1:3] #first 3 elements in a, assign the result to bb #shows bc=a[-1] #take all elements in b except the first one

#and crete a new objecta[c(1,5)] #first and last component in a

There exists a lot of operations that can be performed using vectors, forexample:

w=c(1,2,3,NA,-1,2)which(is.na(w)) #Missing valueswhich.max(w) #Position of the maximumwhich.min(w) #Position of the minumumw>2 #numbers that are bigger than 2?which(w>2) #which numbers are bigger than 2?sort(w) #sort in ascending ordersort(w,decreasing=T) #sorts in decreasing order

SLU,Sweden Short Introduction to R 28/51

Page 29: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 29/51

Page 30: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

Creating matricesMatrices can contain numbers or characters. The creation of matrices isshown in the examples below.

#a)Identity matrix, nxn#Identity matrix of order 4x4Identity=diag(c(1,1,1,1))Identity

#Alternatively...Identity=diag(rep(1,4))Identity

#b)J matrix#J matrix, order 3x3J=matrix(1,nrow=3,ncol=3)J

#c)In general#matrix(data = NA, nrow = 1, ncol = 1,byrow = FALSE, dimnames = NULL)

A=matrix(nrow=3,ncol=3)A[1,]=c(1,2,3)A[2,]=c(4,5,6)A[3,]=c(7,8,9)

SLU,Sweden Short Introduction to R 30/51

Page 31: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

#Alternatively ...A=matrix(c(1:9),nrow=3,ncol=3,byrow=TRUE)A

#Alternatively...A=matrix(c(1,4,7,2,5,8,3,4,8),nrow=3,ncol=3,byrow=FALSE)A

SLU,Sweden Short Introduction to R 31/51

Page 32: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

Matrix operations

#Sum and substractionC=A+JCD=A-JD

#Matrix product (%*%)Dsq=D%*%DDsqDsqA=D%*%D%*%(A)DsqA

#Transpose, use t()t(D)

#Determinant, det()det(D)

#Inverse, use the function solve()InvI=solve(Identidad)InvI

SLU,Sweden Short Introduction to R 32/51

Page 33: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

#Rangk, use the QR decompositionqr(Identidad)qr(Identidad)$rank

Noninvertible=matrix(c(1,2,3,1,2,1,2,4,6),nrow=3,ncol=3,byrow=TRUE)det(Noninvertible)qr(Noninvertible)$rank

SLU,Sweden Short Introduction to R 33/51

Page 34: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects Matrices

We can also extract some elements of the matrix, for example:

A #Shows AA[1,1] #Element in row 1, column 1 in AA[1,] #First row of AA[c(1,2),] #First and second row of AA[-3,] #All rows except the third oneA[,1] #First column of AA[,c(1,3)] #Columns one and third in AA[,-3] #All columns except third one

Note: When we extract a row or column it is automatically converted to avector.

SLU,Sweden Short Introduction to R 34/51

Page 35: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects data.frame

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 35/51

Page 36: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Simple objects data.frame

data.frame

Tables are created using the function data.frame(v1,...,vn), where v1 is thevector 1 and vn is the vector n. The rows usually represents individuals andthe columns covariates.

Examples

ID=c("genO","genB","genZ")subj1=c(10,25,33)subj2=c(NA,34,15)oncogen=c(TRUE,TRUE,FALSE)loc=c(1,30,125)data1=data.frame(ID,subj1,subj2,oncogen,loc)data1

#If you want to display the column names in a#data.frame, use the function namesnames(data1)

#To show or extract a column use the operator $data1$subj2data1$subj2data1$oncogen

SLU,Sweden Short Introduction to R 36/51

Page 37: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

User defined functions

User defined functions

R implements a lot of statistical methodologies using functions. The functionsare organized in libraries. The base library contains all the functions that wehave been using so far. The libraries can be downloaded freely from theinternet.

We can create our own functions for data analysis. The syntax for creating anew function is as follows:

funcion_name=function(arg1,...,argn){function body;return the value;

}

Examples:

SLU,Sweden Short Introduction to R 37/51

Page 38: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

User defined functions

A function to compute f (x) = x2

f=function(x){x^2

}f(2)f(c(1,2,3))

A function to compute∑n

i=1 i

my_sum=function(n){

tmp=c(1:n)sum(tmp)

}#The result should be identical to n(n+1)/2n=100my_sum(n)n*(n+1)/2

SLU,Sweden Short Introduction to R 38/51

Page 39: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs

R includes many functions to produce high quality graphics ready forpublication. We can explore the graphical capabilities of the software includedin the demos. Type demo() in the command prompt and the software willdisplay a list of demos that we can execute, for example:

graphicsimageperspplotmath

demo()demo(graphics) #Some graphical capabilitiesdemo(image) #Working with imagesdemo(persp)demo(plotmath) #Mathematical symbols in graphs

SLU,Sweden Short Introduction to R 39/51

Page 40: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs Plotting user defined functions

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 40/51

Page 41: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs Plotting user defined functions

Plotting user defined functions

To plot user defined functions we can use the functions curve(), or plot(). Wewill give more details about the later in the next slides.

Examples

#1: Plotting f(x)=x^2, -4<=x<=4curve(x^2,-4,4)

#2: Plotting f(x)=-x^3, -4<=x<=4curve(-x^2,-4,4)

#3:op=par(mfrow=c(2,2))curve(x^3-3*x, -2, 2)curve(x^2-2, add = TRUE, col = "violet")plot(cos, xlim =c(-pi,3*pi), n = 1001, col = "blue")chippy=function(x) sin(cos(x)*exp(-x/2))curve(chippy, -8, 7, n=2001)curve(chippy,-8, -5)

#4: Standard normalcurve(1/sqrt(2*pi)*exp(-1/2*x^2),-3,3)

SLU,Sweden Short Introduction to R 41/51

Page 42: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs Plot function

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 42/51

Page 43: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs Plot function

Plot function

One of the most useful function for plotting is the plot function. With thisfunction we can plot points (scatterplot), lines (time series), or functions.

Examples

y<-c(1,2,3,4,5)x<-c(1,4,9,16,25)plot(x,y,main="",ylab="", xlab="")plot(x,y,type="l")plot(dnorm, -3,3,col = "blue")

SLU,Sweden Short Introduction to R 43/51

Page 44: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs More functions for graphs

Contents1 Introduction

¿R?R and other sofwaresManuals and helpSample R session

2 Simple objectsVectorsMatricesdata.frame

3 User defined functions4 Graphs

Plotting user defined functionsPlot functionMore functions for graphs

5 Importing data6 Installing packages7 Questions

SLU,Sweden Short Introduction to R 44/51

Page 45: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Graphs More functions for graphs

More functions for graphs

barplotpiehistogramboxplot

SLU,Sweden Short Introduction to R 45/51

Page 46: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Importing data

Importing data

There are several routines to import data into the R environment. For ASCIIfiles we can use:

1 read.table2 read.csv

The function setwd is useful for setting the working directory so that we donot have to write the entire PATH of a file each time that we want to read it.

R can save and load objects in a native binary format. The functions loadand save can be used to that end.

SLU,Sweden Short Introduction to R 46/51

Page 47: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Importing data

Examples

This data set is from CIMMYT global Wheat breeding program and comprisesphenotypic, genotypic and pedigree information of n = 599 wheat lines. Thedata set was made publicly available by Crossa et al. (2010). Lines wereevaluated for grain yield at four different environments. Each of the lines weregenotyped for p = 1,279 Diversity Array Technology (DArT) markers. At eachmarker two homocygous genotypes were possible and these were coded as0/1. Marker genotypes are given in the object X. Finally a matrix A providesthe pedigree-relationships between lines computed from the pedigree.

SLU,Sweden Short Introduction to R 47/51

Page 48: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Importing data

Continue...

rm(list=ls())

library(doBy)

setwd("~/0. R-Intro/examples")

#Load genotypic dataload("pedigree_markers.RData")

#Load phenotypic datapheno=read.table(file="599_yield_raw-1.prn",header=TRUE)

colnames(pheno)

pheno=pheno[,c(2,5,6)]

out=summaryBy(GY~env+gen1,data=pheno,FUN=mean)

Y=data.frame(yield=out$GY.mean,VAR=out$gen1,ENV=out$env)

SLU,Sweden Short Introduction to R 48/51

Page 49: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Installing packages

Installing packages

Many users all over the world are creating software packages for R,

Figure 10: Packages.

SLU,Sweden Short Introduction to R 49/51

Page 50: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Installing packages

Continue...

A package is just a collection of routines used to perform some calculations.Usually these routines are made available to the user through functions welldocumented.

The function install.packages() is used to install software from theCRAN website, for example:

install.packages("BLR")install.packages("BGLR")install.packagses("doBy")

Once that a package is installed, it should be loaded with the functionlibrary,

library(doBy)library(BGLR)

SLU,Sweden Short Introduction to R 50/51

Page 51: Short Introduction to R - CIMMYTgenomics.cimmyt.org/SLU/Slides Paulino/0. R-Intro/Presenta.pdf · Short Introduction to R Paulino Pérez 1 José Crossa 2 1ColPos-México 2CIMMyT

Questions

Questions?

SLU,Sweden Short Introduction to R 51/51