introduc)on to r -...

25
Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3) University of Liege 20/03/2017 KC - ULg 1

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Introduc)ontoR

KridsadakornChaichoompuGIGA-MedicalGenomics(BIO3)

UniversityofLiege

20/03/2017 KC-ULg 1

Page 2: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Basiccommands•  q() ToquitRenvironment•  x=5 Assignmentoperator•  y<-5 Assignmentoperator•  ls() TolistobjectsinRenvironment•  ?ls() Tocheckhowtouseafunc)on•  getwd() Togetaworkingdirectory•  setwd("New/Directory")

Tosetanewworkingdirectory•  save(x,y,file="mydata.RData")

TosaveobjectsastheRdatafile•  save.image(file="alldata.RData")

TosaveallobjectsastheRdatafile•  load("mydata.RData")

ToloadtheRdatafiletotheworkingspace

20/03/2017 KC-ULg 2

Page 3: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Arithme)coperators•  5+7 Addi)on •  8-3 Subtrac)on•  5*2 Mul)plica)on•  9/2 Division•  (8+3)*4 Parentheses•  2^4 Power•  exp(4) Exponen)alfunc)on•  log(8) NaturalLogarithm•  log10(8) Logarithminbase10•  pi Pinumber

20/03/2017 KC-ULg 3

Page 4: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

LogicaloperatorsThevaluescanbeT,TRUE,F,FALSE•  5<6 lessthan•  5<=6 lessthanorequalto•  5>6 greaterthan•  5>=6 greaterthanorequalto•  5==6 exactlyequalto•  5!=6 notequalto•  !a NOTa•  a|b aORb•  a&b aANDb•  xor(a,b) aXORb•  isTRUE(a) testifXisTRUEExpressionstatement•  if(a==5&&b>5)•  if(a==5||b>5)

20/03/2017 KC-ULg 4

Page 5: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Basicdatatypesclass()-tocheckclassofobject•  Logical TRUE,T,FALSE,F

class(TRUE)•  Numeric 2.4,10,200

class(6.5)•  Integer 1L,0L,-7L

class(-8L)•  Complex 6+3i

class(6 + 3i)•  Character'hello',"I","like",'R'

class('hello')•  Factor

a = as.factor(1)a = as.factor('hello')class(a)

20/03/2017 KC-ULg 5

Page 6: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

VectorTocreatevectors•  a = c(1,2,0,6.6,-2.5) •  b = c("a","b","c") •  c = c(F,T,TRUE,FALSE)

Vectorsandoperators•  a + 5•  a * 2•  c & TRUE•  c | FALSE•  1:5 Vector of 1 to 5•  c(a,1:5) Concatenate 2 vectors

20/03/2017 KC-ULg 6

Page 7: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

MatrixTocreatematricesmatrix(vector,nrow=r,ncol=c,byrow=FALSE)•  a = matrix(1:12, nrow=3, byrow=F)•  b = matrix(1:12, nrow=3, byrow=T)•  c = matrix(runif(12,min=0,max=1), nrow=3, byrow=T)•  d = matrix(sample(c(TRUE,FALSE),12,replace=TRUE),

nrow=3, byrow=T)

Matricesandoperators•  a + 5•  a + b•  t(b) Transpose of matrix•  a * b Element-wise multiplication•  a %*% t(b) Matrix multiplication

20/03/2017 KC-ULg 7

Page 8: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Matrix(2)Toaccesselementsofmatrix•  a[1,1]•  a[,1]•  a[1,]•  a[,2:3]Tonamerowandcolumns•  colnames(a) = c("a","b","c","d")•  rownames(a) = c("1","2","3")Tocombine2matrices•  cbind(a,b)Combine by column•  rbind(a,b)Combine by row

20/03/2017 KC-ULg 8

Page 9: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Dataframe“data.frame”isthecollec)onsofvariableswhichsharemanyoftheproper)esofmatricesandoflists

Tocreatedata.frame•  x = c("Kris", "Jack", "Steve", NA)•  y = c(50,20,60,40)•  z = c(FALSE,TRUE,TRUE,FALSE)•  df = data.frame(x,y,z)•  colnames(df) <- c("name","paid","registered")

Usefulfunc)ons•  df$name•  is.na(df$name) Check all elements if they are NA?•  anyNA(df$name) Is there any NA?•  df$paid * 1.21•  dim(df) Check dimension•  df[which(df$name=="Kris"),] Get specific row

20/03/2017 KC-ULg 9

Page 10: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Dataframe(2)

Tonamerowandcolumns•  colnames(df) = c("1","2","3”)•  rownames(df) = c("a","b","c","d")Tocombine2matrices•  cbind(df,df) Combine by column•  rbind(df,df) Combine by row

20/03/2017 KC-ULg 10

Page 11: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

List

Acollec)onofobjectswhichcanbeindifferentlength•  m = list(car=c("Toyota","Honda","Nissan"),age=c(23,67),single=TRUE)

Toaccessobjects•  m$car•  m$age•  m[[1]]•  m[[2]]

20/03/2017 KC-ULg 11

Page 12: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Conversionfunc)ons

•  as.matrix(df)•  as.data.frame(a)•  as.list(1:5)•  as.integer(1:5)•  as.logical(c(0,1,1,0))•  as.factor(1:5)

20/03/2017 KC-ULg 12

Page 13: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Concatena)onfunc)ons•  c() To combine vectors•  list() To combine lists•  cbind() To combine matrices and data frames by column•  rbind() To combine matrices and data frames by row•  paste("Hello","my","name","is","Kris")

To combine strings•  paste0("Hello","my","name","is","Kris")

To combine strings without spaceTricktodisplaytextonscreen•  str = paste("Hello","my","name","is","Kris","\n")•  cat(str) To display text•  print(str) To display all values as they are

20/03/2017 KC-ULg 13

Page 14: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

ControlFlow

•  if(condi)on)...•  if(condi)on)...else...

•  for(variableinsequence)...•  while(condi)on)...

•  break Tostopitera)on•  next Toskiptonextitera)on

20/03/2017 KC-ULg 14

Page 15: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

IFExamples:

age = 10if (age > 18){

cat("Old\n")}else{

cat("Young\n")}

age = 20if ((age>18) && (age<25)){

cat("Teenager\n")}else{

cat("Other type\n")}

20/03/2017 KC-ULg 15

Page 16: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

FOR

Examples:for (i in 1:10){cat(paste(i,"\n"))

}

name = c("Hello","my","name","is","Kris")for (i in name)cat(paste0(i," "))

20/03/2017 KC-ULg 16

Page 17: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

WHILEExamples:i = 0while (i<5){

print(i)i = i+1

}

i = 0while (i<10){

if (i>5) nextprint(i)i = i+1

}

20/03/2017 KC-ULg 17

Page 18: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Importdelimitedtextfile•  TheformasedtextfilescanbeimportedtoRbythesefunc)ons:

–  Read.table()–  read.csv(),read.csv2()–  read.delim(),read.delim2()

•  Importantparameters:–  file:thenameofinputfile–  header:toindicatewhetherthefirstlinecontainsthenamesofthevariables

ornot–  sep=theseparatorcharacter

•  Trytoimportorange.csv

Downloadfromthecoursewebsite: hsp://www.montefiore.ulg.ac.be/~chaichoompu

•  Example:mydata=read.table(file="orange.csv",sep=",",header=TRUE)head(mydata)

20/03/2017 KC-ULg 18

Page 19: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Exportasdelimitedtextfile•  Youcanusethesefunc)onstoexporttofile

–  write.table(x,file="")–  write.csv()

•  Importantparameters:–  file:thenameofinputfile–  row.names:toindicatewhetherrownameswillbeexportedornot–  col.names:toindicatewhethercolumnnameswillbeexportedornot–  sep:theseparatorcharacter–  quote:toindicatewhethertextwillbequoted(“hello”)

•  Example:write.table(mydata,file="newfile.csv",quote=T,sep="\t",row.name=T,col.name=T)

20/03/2017 KC-ULg 19

Page 20: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

TextdisplayTodisplaytextonscreen•  print(x, ...)•  cat(...)

Concatenatevariables•  paste (...)•  paste0(...)Example:•  dd <- 28•  mm <- "October"•  yy <- 2016•  cat(paste0(dd,mm,yy))•  cat(paste(dd,mm,yy,sep="-"))

20/03/2017 KC-ULg 20

Page 21: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Plots•  Useplot()tocreateasimpleXYplot–  plot(rnorm(10))

•  Inthecompu)ngservers,weneedtosaveplotsasfilesandtransfertoalocalcomputertoview–  pdf(file="./xyplot.pdf")ècreateapdffileinthecurrentworkingdirectory

–  plot(rnorm(10))–  points(rnorm(2),col="red")èadd2reddotstotheplot–  dev.off()èclosethegraphicalsession,allgraphicalfunc)onscalledbeforedev.off()willbesavedtopdffile

•  Ralsosupportstheothertypesofgraphicalfiles–  Check:jpeg(),)ff(),png(),bmp()

20/03/2017 KC-ULg 21

Page 22: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Plovngformul)pledataseriesSingleline:

age=mydata$age[which(mydata$Tree==1)]cir=mydata$circumference[which(mydata$Tree==1)]plot(age,cir,type="o",xlab="Age",ylab="Circumference",col=1)

20/03/2017 KC-ULg 22

500 1000 1500

4060

80100

120

140

Age

Circum

ference

Page 23: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Plovngformul)pledataseries(2)Addmorelines:

trees=sort(unique(mydata$Tree))subtrees=trees[-1]for (item in subtrees){ age=mydata$age[which(mydata$Tree==item)] cir=mydata$circumference[which(mydata$Tree==item)] lines(age,cir,col=item,type="o")}

20/03/2017 KC-ULg 23

500 1000 1500

4060

80100

120

140

Age

Circum

ference

● ●

Page 24: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Mul)pleplotspar(mfrow=c(2,2))plot(rnorm(10),col="red")plot(rnorm(10),col="green")plot(rnorm(10),col="blue")plot(rnorm(10),col="pink")

20/03/2017 KC-ULg 24

2 4 6 8 10

−0.5

0.0

0.5

1.0

Index

rnorm(10)

2 4 6 8 10

−2−1

01

2

Index

rnorm(10)

2 4 6 8 10

−1.0

0.0

1.0

2.0

Index

rnorm(10)

2 4 6 8 10

−1.0

0.0

0.5

1.0

1.5

Index

rnorm(10)

Page 25: Introduc)on to R - bio3.giga.ulg.ac.bebio3.giga.ulg.ac.be/.../downloads/2017/GBIO0015/L1/Introduction_R.… · Introduc)on to R Kridsadakorn Chaichoompu GIGA-Medical Genomics (BIO3)

Wri)ngyourownfunc)onTodefinefunc)on:f1 <- function(param1, param2, ... ){

print(param1)return(param2)

}

NestedFunc)on:f2 <- function(p2,...){ f1 <- function(p1,...){

var1 <- log10(p1) return(var1) }

var2 <- f1(p2)return(var2)

}

20/03/2017 KC-ULg 25