1. Conditional IF, ELSE, ELSE IF

example 1

x <- - 3
if(x < 0){
  print("x is a negative number")
} 
## [1] "x is a negative number"

example 2

y <- 3
if(y < 0){
  print("y is a negative number")
} else {
  print("y is a positive number")
}
## [1] "y is a positive number"

example 3

z <- 0 
if(z < 0){
  print("z is a negative number")
} else if (z == 0) {
  print("z is zero")
} else {
  print("z is a positive number")
}
## [1] "z is zero"

example 4

number <- 100
if (number < 10) {
  if (number < 5) {
    result <- "extra small"
  } else {
    result <- "small"
  }
} else if (number < 100) {
  result <- "medium"
} else {
  result <- "large"
}
print(result)
## [1] "large"

example 5

## Variables related to your last day of recordings
li <- 15
fb <- 9

## Code the control-flow construct
if (li >= 15 & fb >= 15) {
  sms <- 2 * (li + fb)
} else if (li < 10 & fb < 10) {
  sms <- 0.5 * (li + fb)
} else {
  sms <- li + fb
}

## Print the resulting sms to the console
print(sms)
## [1] 24

2. While loop

R의 while문은 다른 언어들과 같이 while(구문) { } 의 구조를 가집니다. 소괄호 안의 결과가 TRUE가 되는 한, 중괄호 안의 로직은 계속하여 반복될 것입니다.

example 1

ctr <- 1
while(ctr <= 7){
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
## [1] "ctr is set to 1"
## [1] "ctr is set to 2"
## [1] "ctr is set to 3"
## [1] "ctr is set to 4"
## [1] "ctr is set to 5"
## [1] "ctr is set to 6"
## [1] "ctr is set to 7"

example 2

# Initialize the speed variable
speed <- 64

# Code the while loop
while (speed > 30) {
  print("Slow down!")
  speed <- speed - 7
}
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
# Print out the speed variable
speed
## [1] 29

example 3: while + if

# Initialize the speed variable
speed <- 64

# Extend/adapt the while loop
while (speed > 30) {
  print(paste("Your speed is", speed))
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 64"
## [1] "Slow down big time!"
## [1] "Your speed is 53"
## [1] "Slow down big time!"
## [1] "Your speed is 42"
## [1] "Slow down!"
## [1] "Your speed is 36"
## [1] "Slow down!"

example 4: while + if + break

speed <- 88

while (speed > 30) {
  print(paste("Your speed is", speed))
  
  # Break the while loop when speed exceeds 80
  if (speed > 80) {
    break
  }
  
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 88"

example 5

Finish the while loop so that it: prints out the triple of i, so 3 * i, at each run. is abandoned with a break if the triple of i is divisible by 8, but still prints out this triple before breaking.

# Initialize i as 1 
i <- 1

# Code the while loop
while (i <= 10) {
  print(3 * i)
  if ((3 * i) %% 8 == 0) {
    print(3 * i)
    break
  }
  i <- i + 1
}
## [1] 3
## [1] 6
## [1] 9
## [1] 12
## [1] 15
## [1] 18
## [1] 21
## [1] 24
## [1] 24

3. For Loop

example 1-1

cities <- c("New York", "Paris",
            "London", "Tokyo",
            "Rio de Janeiro", "Camptown")
for (city in cities) {
  print(city)
}
## [1] "New York"
## [1] "Paris"
## [1] "London"
## [1] "Tokyo"
## [1] "Rio de Janeiro"
## [1] "Camptown"

example 1-2

for(i in 1:length(cities)) {
  print(cities[i])
}
## [1] "New York"
## [1] "Paris"
## [1] "London"
## [1] "Tokyo"
## [1] "Rio de Janeiro"
## [1] "Camptown"

example 2

nyc <- list(pop = 8405837, 
            boroughs = c("Manhattan", "Bronx", "Brooklyn", 
                         "Queens", "Staten Island"), 
            capital = FALSE)

# Loop version 1
for(n in nyc){
    print(n)
}
## [1] 8405837
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## [1] FALSE
# Loop version 2
for(i in 1:length(nyc)){
    print(nyc[i])
}
## $pop
## [1] 8405837
## 
## $boroughs
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## 
## $capital
## [1] FALSE

example 3: for + if

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Code the for loop with conditionals
for (li in linkedin) {
  if ( li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] 2
## [1] "You're popular!"
## [1] 17
## [1] "You're popular!"
## [1] 14

example 4

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Adapt/extend the for loop
for (li in linkedin) {
  if (li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
  
  # Add if statement with break
   if (li > 16){
  print("This is ridiculous, I'm outta here!")
  break
  }
  
  # Add if statement with next
   if (li < 5){
    print("This is too embarrassing!")
    next
  }
  
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] "This is too embarrassing!"
## [1] "You're popular!"
## [1] "This is ridiculous, I'm outta here!"

example 5: for loop from scratch

# Pre-defined variables
rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]]

# Initialize rcount
rcount <- 0

# Finish the for loop
for (char in chars) {
  if(char == "r")
rcount = rcount + 1
if(char == "u")
  break
}

# Print out rcount
print(rcount)
## [1] 5

3. Functions

example 1

triple <- function(x) {
  y <- 3*x
  return(y)
}
triple(3)
## [1] 9

example 2

math_magic <- function(a, b = 1) {
  if(b==0){
    return(0)
  }
  a*b + a/b
}

math_magic(4,0)
## [1] 0

example 3: return(1)

# Define the function hello()
hello <- function() {
  print("Hi there!")
  return(TRUE)
}

# Call the function hello()
hello()
## [1] "Hi there!"
## [1] TRUE

example 4: return (2)

linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Define the interpret function
interpret <- function(num_views) {
  if (num_views > 15) {
    print("You're popular!")
    return(num_views)

  } else {
    print("Try to be more visible!")
    return(0)

  }
}

# Call the interpret function twice
interpret(linkedin[1])
## [1] "You're popular!"
## [1] 16
interpret(linkedin[2])
## [1] "Try to be more visible!"
## [1] 0
interpret(linkedin[3])
## [1] "Try to be more visible!"
## [1] 0
interpret(linkedin[6])
## [1] "You're popular!"
## [1] 17

example 5

# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

# The interpret() can be used inside interpret_all()
interpret <- function(num_views) {
  if (num_views > 15) {
    print("You're popular!")
    return(num_views)
  } else {
    print("Try to be more visible!")
    return(0)
  }
}

# Define the interpret_all() function
# views: vector with data to interpret
# return_sum: return total number of views on popular days?
interpret_all <- function(views, return_sum = T) {
  count <- 0
  for (v in views) {
    count <- count + interpret(v)
  }
  if (return_sum == TRUE) {
    return (count)
  } else {
    return (NULL)
  }
}

# Call the interpret_all() function on both linkedin and facebook
interpret_all(linkedin)
## [1] "You're popular!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] "You're popular!"
## [1] "Try to be more visible!"
## [1] 33
interpret_all(facebook)
## [1] "You're popular!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] "You're popular!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] "Try to be more visible!"
## [1] 33