.chapter5<-function(i=0){ " i Chapter 5: R loops and various conditions - ------------------------------------- 1 Loop: Simple for loops 2 : Double for loops 3 : while loop 4 : infinitive loop 5 : How to stop an infinitive loop 6 : break 7 : Length of a vector and dimension of a matrix 8 : Loops vs. matrix manipulation 9 : Complex examples 10 Conditioins: if stop 11 : if statement 12 : ifelse() function 13 : if esle() function 14 : if else if() function 15 logic 'OR' 16 logic 'and' 17 (a and b) or (c and d) 18 Embedded conditions 19 Videos 20 Links Example #1:>.c5 # see the above list Example #2:>.c5(1) # see the 1st explanation ";.zchapter5(i)} .n5chapter<-20 .zchapter5<-function(i){ if(i==0){ print(.c5) }else{ .printEachQ(5,i,.n5chapter) } } .c5<-.chapter5 .C5EXPLAIN1<-"Simple for loop /////////////////////////// # Example #1: for(i in 1:3) print(i) # Example #2: n<-1:10 for(j in n) print(j) # Example #3: tickers<-c(\"ibm\",\"c\",\"wmt\") for(ticker in tickers)print(ticker) /////////////////////////// " .C5EXPLAIN2<-"Double for loops /////////////////////////// for(i in 1:100){ for(j in 1:30){ # your code here } } /////////////////////////// " .C5EXPLAIN3<-"while loop /////////////////////////// i<-1 whle (i<100){ x<-i+3 print(x) i<-i+1 } /////////////////////////// " .C5EXPLAIN4<-"infinitive loop /////////////////////////// i<-1 whle (i<100){ x<-i+3 print(x) } /////////////////////////// " .C5EXPLAIN5<-"How to stop an infinitive loop /////////////////////////// Click the red Stop icon on the menu bar /////////////////////////// " .C5EXPLAIN6<-"add break clause /////////////////////////// i<-1 while (2==2){ i<-i+3 print(i) if(i>29) break } /////////////////////////// " .C5EXPLAIN7<-"Length of a vector and dimension of a matrix /////////////////////////// x<-seq(1,50,0.123) n<-length(x) dim() function is for matrices /////////////////////////// " .C5EXPLAIN8<-"Loops vs. matrix manipulation /////////////////////////// Sometimes, matrix manipulation could save us lots of time. For example, assume that we have a price vector, such as p<-c(100,102,99,120,98,120) Assume that the price vector is sorted from the oldest to newest. In other words, the first return is (102-100)/100=0.02 Method I: we use a loop n<-length(p) for(i in 1:(n-1)){ r[i]<-p[i+1]/p[i]-1 } Method II: use a matrix manipulation r2<-p[2:n]/p[1:(n-1)]-1 /////////////////////////// " .C5EXPLAIN9<-"Complex examples /////////////////////////// Retrieve the data set of stockMonthly Write two loops to estimate annual risk for each stock > .search(\"stockMont\") DIR NAME 691 data_sas stockmonthly.sas7bdat 692 data_sas stockmonthly65_89.sas7bdat > .download2cTemp(691) # File/data set called stockmonthly.sas7bdat was successfully downloaded # Your saved file/data is ==>c:/temp/stockmonthly.sas7bdat > > library(haven) > x<-read_sas(\"c:/temp/stockmonthly.sas7bdat\") > head(x) # A tibble: 6 x 5 SYMBOL ADJUSTED DATE n RET 1 A 22.8 2008-01-31 20 -0.0672 2 A 20.6 2008-02-29 20 -0.0960 3 A 20.1 2008-03-31 20 -0.0255 4 A 20.3 2008-04-30 22 0.0127 5 A 25.2 2008-05-30 21 0.238 6 A 23.9 2008-06-30 21 -0.0495 > y<-data.frame(x) > head(y) SYMBOL ADJUSTED DATE n RET 1 A 22.80688 2008-01-31 20 -0.06721765 2 A 20.61780 2008-02-29 20 -0.09598336 3 A 20.09242 2008-03-31 20 -0.02548201 4 A 20.34838 2008-04-30 22 0.01273913 5 A 25.18456 2008-05-30 21 0.23766918 6 A 23.93847 2008-06-30 21 -0.04947825 > dim(y) [1] 58681 5 /////////////////////////// " .C5EXPLAIN10<-"if-stop() condition ///////////////////////////////// r<- -0.1 if(r<0) stop('Rate is less than zero!') ///////////////////////////////// " .C5EXPLAIN11<-"if() statement ///////////////////////////////// if(npv>0) print('accept the project') if(condition){ # your code here } ///////////////////////////////// " .C5EXPLAIN12<-"ifelse() function ///////////////////////////////// This is quite similar to the Excel if() condition ifelse(condition, true, false) ifelse(a>0, \"positive\", \"not-positive\") ///////////////////////////////// " .C5EXPLAIN13<-"if-else ///////////////////////////////// if(condition){ }else{ } ///////////////////////////////// " .C5EXPLAIN14<-"if else if() ///////////////////////////////// if(condition1){ }else if(condition2){ }else if(condition3){ }else if(condition4){ }else{ } ///////////////////////////////// " .C5EXPLAIN15<-"logic OR ///////////////////////////////// We use | for logic or if(x>0 | y>0) print('at least one is positive') ///////////////////////////////// " .C5EXPLAIN16<-"Logic and ///////////////////////////////// We use & for logic or if(x>0 & y>0) print('Both are positive') ///////////////////////////////// " .C5EXPLAIN17<-" (a and b) or (d and e) ///////////////////////////////// a<-T b<-T d<-F e<-T if((a==T & b==T) | (d==T & e==T)){ print('(a and b) or (d and e)') }else{ print('not correct') } ///////////////////////////////// " .C5EXPLAIN18<-"Embedded conditions ///////////////////////////////// > a<-90 > if(a) print('haha') [1] \"haha\" > a<-0 > if(a) print('haha') > a<--9 > if(a) print('haha') [1] \"haha\" ///////////////////////////////// " .C5EXPLAIN19<-"Videos /////////////////////////// Hutson,Gary, 2017, R Programming - For Loops (v11k,s673,t12:18) https://www.youtube.com/watch?v=50Il-XNEZng Math Partner, 2017, R Basics 6 - While and For Loops (v5k,s4k,t12:10) https://www.youtube.com/watch?v=LNpg8jgGex4 Webster, Richard, 2015, For loops (v77k,s5k,t11:33) https://www.youtube.com/watch?v=h987LWDvqlQ DataCamp, 2015, R Tutorial - How To Use Conditional Statements in R (v42k,s84k,t4:26) https://www.youtube.com/watch?v=qvSKipqzg3U R-programming Library, 2018, ifelse() function in R (v4k,s675,t3:26) https://www.youtube.com/watch?v=FG_38X1ZMoc Maths Partner, 2017, R Basics 8 - If Statements with Multiple Conditions (v14k,s4k,t10:25) https://www.youtube.com/watch?v=a2vaD_EqTNE /////////////////////////// " .C5EXPLAIN20<-"Links /////////////////////////// Carpentries, 2018, Loops in R https://swcarpentry.github.io/r-novice-inflammation/15-supp-loops-in-depth/ Fanara,Carlo, 2018, A Tutorial on Loops in R - Usage and Alternatives https://www.datacamp.com/tutorial/tutorial-on-loops-in-r R if else Statement https://www.datamentor.io/r-programming/if-else-statement/ Vries, Andrie de and Joris Meys,How to use R if condition https://www.dummies.com/programming/r/how-to-use-if-statements-in-r/ /////////////////////////// "