Last active
November 13, 2018 11:01
-
-
Save viniciusmss/8b0913a62c458d88b6d122d76dd5b9ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# Comparison of numerics | |
-6 * 5 + 2 >= -10 + 1 | |
# Comparison of character strings | |
"raining" <= "raining dogs" | |
# Comparison of logicals | |
TRUE > FALSE | |
# Initiliaze the linkedin and facebook vectors | |
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 | |
# What about matrices? | |
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 | |
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 | |
# 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 | |
!TRUE | |
!(5 > 3) | |
!!FALSE | |
x <- 5 | |
y <- 7 | |
!(!(x < 4) & !!!(y > 12)) | |
# 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") | |
} | |
# Write the if statement for num_views | |
if (num_views > 15) { | |
print("You're popular!") | |
} | |
# 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") | |
} | |
# Control structure for num_views | |
if (num_views > 15) { | |
print("You're popular!") | |
} else { | |
print("Try to be more visible!") | |
} | |
# 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") | |
} | |
# 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!") | |
} | |
# Initialize the speed variable | |
speed <- 64 | |
# Code the while loop | |
while (speed > 30) { | |
print("Slow down!") | |
speed <- speed - 7 | |
} | |
# Print out the speed variable | |
speed | |
# 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 | |
} | |
} | |
# Print out the speed variable | |
speed | |
# Initialize the speed variable | |
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 | |
} | |
} | |
# The linkedin vector has already been defined for you | |
linkedin <- c(16, 9, 13, 5, 2, 17, 14) | |
# Loop version 1 | |
for (views in linkedin) { | |
print(views) | |
} | |
# Loop version 2 | |
for (i in 1:length(linkedin)) { | |
print(linkedin[i]) | |
} | |
primes_list <- list(2, 3, 5, 7, 11, 13) | |
# loop version 1 | |
for (p in primes_list) { | |
print(p) | |
} | |
# loop version 2 | |
for (i in 1:length(primes_list)) { | |
print(primes_list[[i]]) | |
} | |
ttt <- matrix(c("O", NA, "X", NA, "O", "O", "X", NA, "X"), byrow=T, nrow=3) | |
# define the double for loop | |
for (i in 1:nrow(ttt)) { | |
for (j in 1:ncol(ttt)) { | |
print(paste("On row", i, "and column", j, "the board contains", ttt[i,j])) | |
} | |
} | |
# 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) | |
} | |
# Pre-defined variables | |
rquote <- "r's internals are irrefutably intriguing" | |
chars <- strsplit(rquote, split = "")[[1]] # This splits the string into each of its characters | |
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment