Introduction

In this tutorial, we will try to understand some fundamental control structures used in statistical programming. In the beginning, we will separately analyze different control structures. The ultimate goal is to combine these methods to solve all our computing and life problems. If you can imagine it, R can do it. Programming R is downright magical. If Morpheus calls, pick up the phone.

Part 1: If-Else Statements

Chunk 1: Illustration of If

x = 3
if(x > 0){
  print(log(x))
}

x = -3
if(x > 0){
  print(log(x))
}

Chunk 2: Illustration of If-Else

x = 3
if(x > 0){
  print(log(x))
} else{
  message("Unable to Take Logarithm")
}

x = -3
if(x > 0){
  print(log(x))
} else {
  message("Unable to Take Logarithm")
}

Chunk 3: Potential Problem of If-Else Statements

x = BLANK
if(x > 0){
  print(log(x))
}

if(x > 0){
  print(log(x))
} else{
  message("Unable to Take Logarithm")
}

Chunk 4: Fixing Potential Problem in Chunk 3

x=-3

if(is.numeric(x)){
  if(x > 0){
    print(log(x))
  } else{
    message("Unable to Take Logarithm")
  }
} else{
  message("No Strings Attached")
}

Chunk 5: Vectorized Version with ifelse()

x=c(-1,3,200)
print(log(x))

y1 =  if(x > 0){
        log(x)
      } else{
        NA
      }
print(y1)

y2 = ifelse(x>0,log(x),NA)
print(y2)

Chunk 6: Nested ifelse() Statements

x=rnorm(1000,mean=0,sd=1)
y=ifelse(abs(x)<1,"Within 1 SD",ifelse(abs(x)>2,"Far Far Away","Between 1 and 2 SD"))
y.fct=factor(y,levels=c("Within 1 SD","Between 1 and 2 SD","Far Far Away"))
ggplot() +
  geom_bar(aes(x=y.fct),fill="lightskyblue1") +
  theme_minimal()

Part 2: Loops

Chunk 1: Checking Geometric Series Proof with for loop

a=2 #Any Number
r=-0.5 #Any Number Between -1 and 1: abs(r)<1

theoretical.limit=a/(1-r)

START=a

FINISH.1 = START + a*r^1

FINISH.2 = FINISH.1 + a*r^2

FINISH.3 = FINISH.2 + a*r^3

FINISH.10 = a 
for(k in 1:10){
  FINISH.10=FINISH.10+a*r^k
}

FINISH.100 = a 
for(k in 1:100){
  FINISH.100=FINISH.100+a*r^k
}

DATA = tibble(k=c(1,2,3,10,100,"Infinity"),
            SUMMATION=c(FINISH.1,FINISH.2,FINISH.3,
                        FINISH.10,FINISH.100,
                        theoretical.limit))
print(DATA)

ABSOLUTE.ERROR = abs(FINISH.100-theoretical.limit)
print(ABSOLUTE.ERROR)

Chunk 2: Checking Geometric Series Proof with while loop

a=2
r=0.5

FINISH=a
k=0
while(abs(FINISH-a/(1-r)) > 1e-10) {
  k=k+1
  FINISH = FINISH + a*r^k
  if(k>100) break
}
print(c(k,FINISH))

Chunk 3: Saving Steps in Geometric Series for Figure

a=BLANK
r=BLANK
theoretical.limit=a/(1-r)

K=100 #How Many Steps Do You Want to Save?

summation=rep(NA,(K+1))
summation[1]=a
for (k in 1:K) {
  summation[k+1]=summation[k] + a*r^k
}

ggplot() +
  geom_line(aes(x=1:(K+1),y=summation)) +
  geom_hline(yintercept=theoretical.limit,
             linetype="dashed")