DC Intermediate R - Conditionals and Control Flow

# DC Intermediate R
DC Intermediate R - Conditionals and Control Flow
DC Intermediate R - Loops
DC Intermediate R - Functions
DC Intermediate R - The apply family
DC Intermediate R - Utilities

1 Relational Operators[ | ]

2 Equality[ | ]

# Comparison of logicals
TRUE == FALSE
## [1] FALSE

# Comparison of numerics
-6 * 14 != 17 - 101
## [1] FALSE

# Comparison of character strings
"useR" == "user"
## [1] FALSE

# Compare a logical with a numeric
TRUE == 1
## [1] TRUE

3 Greater and less than[ | ]

# Comparison of numerics
-6 * 5 + 2 >= -10 + 1
## [1] FALSE

# Comparison of character strings
"raining" <= "raining dogs"
## [1] TRUE

# Comparison of logicals
TRUE > FALSE
## [1] TRUE

4 Compare vectors[ | ]

# 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)

# Popular days
linkedin > 15
## [1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE

# Quiet days
linkedin <= 5
## [1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE

# LinkedIn more popular than Facebook
linkedin > facebook
## [1] FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE

5 Compare matrices[ | ]

# The social data has been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)

# When does views equal 13?
views == 13
##       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
## [1,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

# When is views less than or equal to 14?
views <= 14
##       [,1] [,2] [,3]  [,4] [,5]  [,6] [,7]
## [1,] FALSE TRUE TRUE  TRUE TRUE FALSE TRUE
## [2,] FALSE TRUE TRUE FALSE TRUE  TRUE TRUE

6 Logical Operators[ | ]

7 & and |[ | ]

# The linkedin and last variable are already defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
last <- tail(linkedin, 1)

# Is last under 5 or above 10?
last < 5 | last > 10
## [1] TRUE

# Is last between 15 (exclusive) and 20 (inclusive)?
last > 15 & last <= 20
## [1] 14

8 & and | (2)[ | ]

# The social data (linkedin, facebook, views) has been created for you

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10
## [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE

# When were one or both visited at least 12 times?
linkedin >= 12 | facebook >= 12
## [1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE

# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14
##       [,1]  [,2]  [,3]  [,4]  [,5]  [,6] [,7]
## [1,] FALSE FALSE  TRUE FALSE FALSE FALSE TRUE
## [2,] FALSE FALSE FALSE FALSE FALSE  TRUE TRUE

9 Reverse the result: ![ | ]

x <- 5
y <- 7
!(!(x < 4) & !!!(y > 12))
## [1] FALSE

10 Blend it all together[ | ]

# li_df is pre-loaded in your workspace

# Select the second column, named day2, from li_df: second
second <- li_df$day2

# Build a logical vector, TRUE if value in second is extreme: extremes
extremes <- (second > 25 | second < 5)

# Count the number of TRUEs in extremes
sum(extremes)
## [1] 16

# Solve it with a one-liner
sum(second > 25 | second < 5)
## [1] 16

11 Conditional Statements[ | ]

12 The if statement[ | ]

# Variables related to your last day of recordings
medium <- "LinkedIn"
num_views <- 14

# Examine the if statement for medium
if (medium == "LinkedIn") {
  print("Showing LinkedIn information")
}
## [1] "Showing LinkedIn information"

# Write the if statement for num_views
if (num_views > 15) {
  print("You're popular!")
}

13 Add an else[ | ]

# Variables related to your last day of recordings
medium <- "LinkedIn"
num_views <- 14

# Control structure for medium
if (medium == "LinkedIn") {
  print("Showing LinkedIn information")
} else {
  print("Unknown medium")
}
## [1] "Showing LinkedIn information"

# Control structure for num_views
if (num_views > 15) {
  print("You're popular!")
} else {
  print("Try to be more visible!")
}
## [1] "Try to be more visible!"

14 Customize further: else if[ | ]

# Variables related to your last day of recordings
medium <- "LinkedIn"
num_views <- 14

# Control structure for medium
if (medium == "LinkedIn") {
  print("Showing LinkedIn information")
} else if (medium == "Facebook") {
  # Add code to print correct string when condition is TRUE
  print("Showing Facebook information")
} else {
  print("Unknown medium")
}
## [1] "Showing LinkedIn information"

# Control structure for num_views
if (num_views > 15) {
  print("You're popular!")
} else if (num_views <= 15 & num_views > 10) {
  # Add code to print correct string when condition is TRUE
  print("Your number of views is average")
} else {
  print("Try to be more visible!")
}
## [1] "Your number of views is average"

15 Else if 2.0[ | ]

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

16 Take control![ | ]

# 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
sms
## [1] 24
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}