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
Short Introduction to R Paulino Prez 1 Jos Crossa 2 1 ColPos-Mxico 2 - - PowerPoint PPT Presentation
Short Introduction to R Paulino Prez 1 Jos Crossa 2 1 ColPos-Mxico 2 CIMMyT-Mxico September, 2014. SLU,Sweden Short Introduction to R 1/51 Contents Introduction 1 2 Simple objects 3 User defined functions 4 Graphs 5 Importing
1ColPos-México 2CIMMyT-México
SLU,Sweden Short Introduction to R 1/51
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 2/51
Introduction ¿R?
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 3/51
Introduction ¿R?
SLU,Sweden Short Introduction to R 4/51
Introduction R and other sofwares
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 5/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 6/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 7/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 8/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 9/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 10/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 11/51
Introduction R and other sofwares
SLU,Sweden Short Introduction to R 12/51
Introduction Manuals and help
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 13/51
Introduction Manuals and help
SLU,Sweden Short Introduction to R 14/51
Introduction Manuals and help
SLU,Sweden Short Introduction to R 15/51
Introduction Sample R session
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 16/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 17/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 18/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 19/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 20/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 21/51
Introduction Sample R session
SLU,Sweden Short Introduction to R 22/51
Simple objects
SLU,Sweden Short Introduction to R 23/51
Simple objects
SLU,Sweden Short Introduction to R 24/51
Simple objects Vectors
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 25/51
Simple objects Vectors
a=c(1,2,3,4,5) a b=c("a","b","c") b d=1:10 d e=seq(1,10,by=0.5) e f=seq(1,10,length.out=20) f g=rep(10,3) g h=c(e,f) h
SLU,Sweden Short Introduction to R 26/51
Simple objects Vectors
a=c(1,2,3) b=c(2,3,5) a+b #sum a and b a-b #a-b a*b #element wise product a^b #power function 3*a+2*b #product and sum a/b #element wise quotient a^2 #takes the square of each element
exp(a) #Exponential function log(a) #logarithm function d=sqrt(a)+log(b) #square root and logarithm d #shows d
SLU,Sweden Short Introduction to R 27/51
Simple objects Vectors
a=c(1,2,5,7,9) b=a[1:3] #first 3 elements in a, assign the result to b b #shows b c=a[-1] #take all elements in b except the first one #and crete a new object a[c(1,5)] #first and last component in a
w=c(1,2,3,NA,-1,2) which(is.na(w)) #Missing values which.max(w) #Position of the maximum which.min(w) #Position of the minumum w>2 #numbers that are bigger than 2? which(w>2) #which numbers are bigger than 2? sort(w) #sort in ascending order sort(w,decreasing=T) #sorts in decreasing order
SLU,Sweden Short Introduction to R 28/51
Simple objects Matrices
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 29/51
Simple objects Matrices
#a)Identity matrix, nxn #Identity matrix of order 4x4 Identity=diag(c(1,1,1,1)) Identity #Alternatively... Identity=diag(rep(1,4)) Identity #b)J matrix #J matrix, order 3x3 J=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
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
Simple objects Matrices
#Sum and substraction C=A+J C D=A-J D #Matrix product (%*%) Dsq=D%*%D Dsq DsqA=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
Simple objects Matrices
#Rangk, use the QR decomposition qr(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
Simple objects Matrices
A #Shows A A[1,1] #Element in row 1, column 1 in A A[1,] #First row of A A[c(1,2),] #First and second row of A A[-3,] #All rows except the third one A[,1] #First column of A A[,c(1,3)] #Columns one and third in A A[,-3] #All columns except third one
SLU,Sweden Short Introduction to R 34/51
Simple objects data.frame
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 35/51
Simple objects data.frame
ID=c("genO","genB","genZ") subj1=c(10,25,33) subj2=c(NA,34,15)
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 names names(data1) #To show or extract a column use the operator $ data1$subj2 data1$subj2 data1$oncogen
SLU,Sweden Short Introduction to R 36/51
User defined functions
funcion_name=function(arg1,...,argn) { function body; return the value; } Examples:
SLU,Sweden Short Introduction to R 37/51
User defined functions
f=function(x) { x^2 } f(2) f(c(1,2,3))
i=1 i my_sum=function(n) { tmp=c(1:n) sum(tmp) } #The result should be identical to n(n+1)/2 n=100 my_sum(n) n*(n+1)/2
SLU,Sweden Short Introduction to R 38/51
Graphs
demo() demo(graphics) #Some graphical capabilities demo(image) #Working with images demo(persp) demo(plotmath) #Mathematical symbols in graphs
SLU,Sweden Short Introduction to R 39/51
Graphs Plotting user defined functions
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 40/51
Graphs Plotting user defined functions
#1: Plotting f(x)=x^2, -4<=x<=4 curve(x^2,-4,4) #2: Plotting f(x)=-x^3, -4<=x<=4 curve(-x^2,-4,4) #3:
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 normal curve(1/sqrt(2*pi)*exp(-1/2*x^2),-3,3)
SLU,Sweden Short Introduction to R 41/51
Graphs Plot function
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 42/51
Graphs Plot function
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
Graphs More functions for graphs
1
2
3
4
5
6
7
SLU,Sweden Short Introduction to R 44/51
Graphs More functions for graphs
SLU,Sweden Short Introduction to R 45/51
Importing data
1
2
SLU,Sweden Short Introduction to R 46/51
Importing data
SLU,Sweden Short Introduction to R 47/51
Importing data
rm(list=ls()) library(doBy) setwd("~/0. R-Intro/examples") #Load genotypic data load("pedigree_markers.RData") #Load phenotypic data pheno=read.table(file="599_yield_raw-1.prn",header=TRUE) colnames(pheno) pheno=pheno[,c(2,5,6)]
Y=data.frame(yield=out$GY.mean,VAR=out$gen1,ENV=out$env)
SLU,Sweden Short Introduction to R 48/51
Installing packages
SLU,Sweden Short Introduction to R 49/51
Installing packages
install.packages("BLR") install.packages("BGLR") install.packagses("doBy")
library(doBy) library(BGLR)
SLU,Sweden Short Introduction to R 50/51
Questions
SLU,Sweden Short Introduction to R 51/51