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 Equality

# Comparison of logicals
TRUE == FALSE

# Comparison of numerics
-6 * 14 != 17 - 101

# Comparison of character strings
"useR" == "user"

# Compare a logical with a numeric
TRUE == 1

2 Greater and less than

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

# Comparison of character strings
"raining" <= "raining dogs"

# Comparison of logicals
TRUE > FALSE

3 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

# Quiet days
linkedin <= 5

# LinkedIn more popular than Facebook
linkedin > facebook

4 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

# When is views less than or equal to 14?
views <= 14

5 & 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

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

6 & and | (2)

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

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10

# When were one or both visited at least 12 times?
linkedin >= 12 | facebook >= 12

# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14

7 Reverse the result: !

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

8 Blend it all together

9 Conditional Statements

10 The if statement

11 Add an else

12 Customize further: else if

13 Else if 2.0

14 Take control!

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}