Biomedical Engineering Theory And Practice/R Programming

R Programming

edit

R Programming Features

edit

Programming Features

edit
For Loop
edit
> for(i in 1:4){print(i*(i+1))}
[1] 2
[1] 6
[1] 12
[1] 20
If statement
edit
> fac1 <- function(x)
+ { 
+  f<-1
+  if(x<2) return(1)
+  for(i in 2:x)
+  { f<-f*i}
+  f
+ }
> sapply(0:4,fac1)
[1]  1  1  2  6 24
While Loop
edit
> facto2<-function(x)
+ {
+ f<-1
+ t<-x
+ while(t>1){
+ f<-f*t
+ t<-t-1
+ }
+ return(f)
+ }
> sapply(0:4,facto2)
[1]  1  1  2  6 24
Repeat Loop
edit
> facto3<-function(x)
+ {
+ f<-1
+ t<-x
+ repeat
+ {
+ if(t<2) break
+ f<-f*t
+ t<-t-1
+ }
+ return(f)
+ }
> sapply(0:6,facto3)
[1]   1   1   2   6  24 120 720
Ifelse statement
edit
> data(LakeHuron)
> LakeHuron
Time Series:
Start = 1875 
End = 1972 
Frequency = 1 
 [1] 580.38 581.86 580.97 580.80 579.79 580.39 580.42 580.82 581.40 581.32
[11] 581.44 581.68 581.17 580.53 580.01 579.91 579.14 579.16 579.55 579.67
[21] 578.44 578.24 579.10 579.09 579.35 578.82 579.32 579.01 579.00 579.80
[31] 579.83 579.72 579.89 580.01 579.37 578.69 578.19 578.67 579.55 578.92
[41] 578.09 579.37 580.13 580.14 579.51 579.24 578.66 578.86 578.05 577.79
[51] 576.75 576.75 577.82 578.64 580.58 579.48 577.38 576.90 576.94 576.24
[61] 576.84 576.85 576.90 577.79 578.18 577.51 577.23 578.42 579.61 579.05
[71] 579.26 579.22 579.38 579.10 577.95 578.12 579.75 580.85 580.41 579.96
[81] 579.61 578.76 578.18 577.21 577.13 579.10 578.25 577.91 576.89 575.96
[91] 576.80 577.68 578.38 578.52 579.74 579.31 579.89 579.96
> meanLakeHuron<-mean(LakeHuron)
> ifelse(LakeHuron > meanLakeHuron,"Higher","Lower")
Time Series:
Start = 1875 
End = 1972 
Frequency = 1 
 [1] Higher Higher Higher Higher Higher Higher Higher Higher Higher Higher
[11] Higher Higher Higher Higher Higher Higher Higher Higher Higher Higher
[21]  Lower  Lower Higher Higher Higher  Lower Higher Higher  Lower Higher
[31] Higher Higher Higher Higher Higher  Lower  Lower  Lower Higher  Lower
[41]  Lower Higher Higher Higher Higher Higher  Lower  Lower  Lower  Lower
[51]  Lower  Lower  Lower  Lower Higher Higher  Lower  Lower  Lower  Lower
[61]  Lower  Lower  Lower  Lower  Lower  Lower  Lower  Lower Higher Higher
[71] Higher Higher Higher Higher  Lower  Lower Higher Higher Higher Higher
[81] Higher  Lower  Lower  Lower  Lower Higher  Lower  Lower  Lower  Lower
[91]  Lower  Lower  Lower  Lower Higher Higher Higher Higher

Text Functions

edit
> x<-"Hand"
> y<-"some"
> z<-c(x,y)
> z
[1] "Hand" "some"
> paste(x,y)
[1] "Hand some"
> paste(x,y,sep="")
[1] "Handsome"
> pets <-c("cat","dog","turtle","rabbit")
> nchar(pets)
[1] 3 3 6 6
> class(pets)
[1] "character"
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> which(letters=="d")
> DNA <-"Deoxyribonucleic acid"
> nchar(DNA)
[1] 21
> q<-character(nchar(DNA))
> for(i in 1:21){q[i]<-substr(DNA,1,i)}
> q
 [1] "D"                     "De"                    "Deo"                  
 [4] "Deox"                  "Deoxy"                 "Deoxyr"               
 [7] "Deoxyri"               "Deoxyrib"              "Deoxyribo"            
[10] "Deoxyribon"            "Deoxyribonu"           "Deoxyribonuc"         
[13] "Deoxyribonucl"         "Deoxyribonucle"        "Deoxyribonuclei"      
[16] "Deoxyribonucleic"      "Deoxyribonucleic "     "Deoxyribonucleic a"   
[19] "Deoxyribonucleic ac"   "Deoxyribonucleic aci"  "Deoxyribonucleic acid"
> AI <-"Artificial Intelligence"
> nchar(AI)
[1] 23
> strsplit(AI,split=character(0))
[[1]]
 [1] "A" "r" "t" "i" "f" "i" "c" "i" "a" "l" " " "I" "n" "t" "e" "l" "l" "i" "g"
[20] "e" "n" "c" "e"
> toupper(AI)
[1] "ARTIFICIAL INTELLIGENCE"
> tolower(AI)
[1] "artificial intelligence"

Pattern Matching

edit
> x<-c("P","L","P","T","A","H","G","A")
> y<-c("P","T")
> match(x,y)
[1]  1 NA  1  2 NA NA NA NA

Regular Expression

edit
> y<-read.csv("http://data.illinois.gov/api/views/876x-r9pn/rows.csv",head=TRUE)
> str(y)
'data.frame':	212 obs. of  8 variables:
 $ Home.Nursing.Agency    : Factor w/ 202 levels "1776 Home Care, LLC",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Address                : Factor w/ 212 levels "10001 Derby Lane, Suite 201",..: 191 179 19 83 107 170 126 82 46 31 ...
 $ City                   : Factor w/ 115 levels "Addison","Alsip",..: 42 37 38 86 106 103 3 66 31 27 ...
 $ Zip                    : int  60137 60423 61036 60068 62701 60177 62002 61265 60201 62025 ...
 $ County                 : Factor w/ 40 levels "Adams","Bond",..: 10 38 16 7 34 17 26 32 7 21 ...
 $ Phone                  : Factor w/ 212 levels "(201) 403-9302",..: 69 159 169 197 10 183 49 27 192 209 ...
 $ License..              : int  4000481 4000465 4000364 4000140 4000102 4000348 4000227 4000335 4000473 4000470 ...
 $ License.Expiration.Date: Factor w/ 19 levels "01/31/16","01/31/17",..: 4 17 12 4 19 7 19 15 7 3 ...
> attach(y)
> grep("R",City)
 [1]   4  23  58  66  69 103 127 136 138 161 165
> City[grep("R",City)]
 [1] Park Ridge      Rockford        Rock Falls      Rolling Meadows
 [5] Robinson        Rockford        Rock Island     Rosemont       
 [9] Rockford        Rockford        Rosemont       
115 Levels: Addison Alsip Alton Aurora Barrington Bloomington ... Wilmette
> as.vector(City[grep("R",as.character(City))])
 [1] "Park Ridge"      "Rockford"        "Rock Falls"      "Rolling Meadows"
 [5] "Robinson"        "Rockford"        "Rock Island"     "Rosemont"       
 [9] "Rockford"        "Rockford"        "Rosemont"
Metacharacter Description Metacharacter Description
. Matches any single character * Matches zero or more of the previous items
+ Matches one or more of the previous items ? Matches zero or one of the previous items
^ Matches the beginnning of the string $ Matches at the end of the string
\ The Or operator \ The escape character
[] Matches the single character that is contained within the square bracket [^] Matches the single character that is not contained within the square bracket.
() Groups character into substring
> as.vector(City[grep("y$",as.character(City))])
[1] "Quincy"       "Calumet City" "Harvey" 

> as.vector(City[grep("^[D-E]",as.character(City))])
 [1] "Evanston"       "Edwardsville"   "Edwardsville"   "Downers Grove" 
 [5] "Des Plaines"    "Elmwood Park"   "Des Plaines"    "Eldorado"      
 [9] "Evanston"       "Dixon"          "Deerfield"      "East St. Louis"
[13] "Elmhurst"       "Des Plaines"    "East Peoria"    "Dixon"         
[17] "Des Plaines"    "Decatur"        "Evergreen Park" "Des Plaines"   
[21] "Des Plaines"  

> as.vector(City[-grep("[a-t]$",as.character(City))])
[1] "Quincy"       "Glenview"     "Calumet City" "Peru"         "Harvey"      
[6] "Bridgeview"   "Glenview"

> as.vector(City[-grep("[a-j n-z]$",as.character(City))])
 [1] "Oakbrook"       "Oak Park"       "Oak Brook"      "Oak Brook"     
 [5] "Elmwood Park"   "Bolingbrook"    "Orland Park"    "Highland Park" 
 [9] "Oak Brook"      "Evergreen Park" "Tinley Park"    "Tinley Park"   
[13] "Villa Park"     "Northbrook"     "Carol Stream"   "Tinley Park"   
[17] "Oak Park"       "Tinley Park"    "Oakbrook"       "Oak Park"      
[21] "Tinley Park"    "Burbank"        "Orland Park"    "Oak Park"      
[25] "Bolingbrook" 

> as.vector(City[grep("^.y",as.character(City))])
[1] "Sycamore"

> as.vector(City[grep("^..y",as.character(City))])
[1] "Crystal Lake"   "Olympia Fields" "Crystal Lake"   "Olympia Fields"
[5] "Olympia Fields"

> as.vector(City[grep("^.{5}y",as.character(City))])
[1] "Quincy"      "Harvey"      "Jerseyville" "Tinley Park" "Tinley Park"
[6] "Tinley Park" "Tinley Park" "Tinley Park"

Substituting text & Location of a pattern

edit
> text<-c("Amino","Peptide","Protein","Hormone")
> sub("o","*",text)
[1] "Amin*"   "Peptide" "Pr*tein" "H*rmone"
> gsub("o","*",text)
[1] "Amin*"   "Peptide" "Pr*tein" "H*rm*ne"
> gsub("^.","*",text)
[1] "*mino"   "*eptide" "*rotein" "*ormone"

> aliphaticaminoacid<-c("Glycine","Alanine","Valine","Leucine","Isoleucine")
> regexpr("i",aliphaticaminoacid)
[1] 5 5 4 5 8
attr(,"match.length")
[1] 1 1 1 1 1
attr(,"useBytes")
[1] TRUE

> grep("I",aliphaticaminoacid)
[1] 5
> aliphaticaminoacid[grep("I",aliphaticaminoacid)]
[1] "Isoleucine"

Time and Date Functions

edit
> Sys.time()
[1] "2015-09-12 06:31:28 EDT"
> as.numeric(Sys.time())
[1] 1442053902
> time.list<-as.POSIXlt(Sys.time())
> time.list
[1] "2015-09-12 06:32:31 EDT"
> unlist(time.list)
      sec       min      hour      mday       mon      year      wday      yday 
 31.80883  32.00000   6.00000  12.00000   8.00000 115.00000   6.00000 254.00000 
    isdst 
  1.00000 
> time.list<-as.POSIXct(Sys.time())
> time.list
[1] "2015-09-12 06:33:07 EDT"
> unlist(time.list)
[1] "2015-09-12 06:33:07 EDT"
> t1<-"2007-05-04"
> t2<-Sys.time()
> difftime(t1,t2)
Time difference of -3053.278 days
> round(difftime(t1,t2))
Time difference of -3053 days

> t1<-as.POSIXlt("2007-05-04")
> t2<-as.POSIXlt(Sys.time())
> t1-t2
Time difference of -3053.277 days

How to write R Functions

edit

Function Definition

edit
Function-name<-Function(argument1, argument2,...)
{
<Body>
return (expression)
}

variable<-Function-name(argument1,argument2,...

Example of Function

edit
  • Mean
> arithmatic.mean<-function(x)
+ {
+ return(sum(x)/length(x))
+ }
> y<-c(5,5,9,3,22,10)
> mean(y)
[1] 9
  • Median
> arithmatic.med<-function(x)
+ {
+ odd.even<-length(x)%%2
+ if(odd.even==0)
+ {
+ a<-sort(x)[length(x)/2]
+ b<-sort(x)[1+length(x)/2]
+ return((a+b)/2)
+ }
+ else
+ {
+ a<-sort(x)[ceiling(length(x)/2)]
+ return(a)
+ }
+ }
> y<-c(5,9,5,7,9)
> arithmatic.med(y)
[1] 7

Return Values

edit
> oddcount<-function(x)
+ {
+ k<-0
+ for(n in x)
+ {
+ if(n%%2==1) { k<-k+1 }
+ }
+ return(k)
+ }
> y<-c(2,9,11,22,21,5)
> oddcount(y)
[1] 4
> oddcount<-function(x)
+ {
+ k<-0
+ for(n in x)
+ {
+ if(n%%2==1){ k<-k+1 }
+ }
+ k
+ }
> y<-c(2,9,11,22,21,5)
> oddcount(y)
[1] 4

Binary Operations

edit
  • It creates a new operator using function definition. It starts with % and end with %.
> "%a5b%" <-function(a,b) return(a+5*b)
> 5 %a5b% 8
[1] 45

Anonymous Functions

edit
  • A single line function can be written without the name.
> inc <- function(x) return(3*x+10)
> inc(2)
[1] 16
> x<-0:5
> y<-10
> z<-x^2+y
> z
[1] 10 11 14 19 26 35
> (function(x,y){z<-x^2+y;return(z)})(0:5,10)
[1] 10 11 14 19 26 35

Functional Programming Features

edit
Scope
edit

-Functions can be defined inside the function. Variables in Functions inside the function is not the part of top-level environment.

Functions are objects
edit
Functions have no side effects
edit
Closure
edit

Recursion

edit

Switch Function

edit
> total<-function(y,measure)
+ {
+ switch(measure,
+ a=mean(y),
+ b=median(y),
+ c=max(y),
+ stop("Measure is not included")
+ )
+ }
> x<-c(2,5,9,11,13,15,8)
> total(x,"a")
[1] 9
> total(x,"b")
[1] 9
> total(x,"c")
[1] 15
> total(x,"4")
Error in total(x, "4") : Measure is not included

Practise

edit

References

edit