Skip to content

Instantly share code, notes, and snippets.

@viniciusmss
Last active November 13, 2018 11:01
Show Gist options
  • Save viniciusmss/8b0913a62c458d88b6d122d76dd5b9ce to your computer and use it in GitHub Desktop.
Save viniciusmss/8b0913a62c458d88b6d122d76dd5b9ce to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## R Prep Minicourse\n",
"### Week 10: Control Flow (Ifs and Loops)\n",
"\n",
"**Credits:** [Datacamp's Intermediate R Course](https://www.datacamp.com/courses/intermediate-r)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"To be TRUE or not be TRUE, that's the question. We'll focus on learning about relational operators to see how R objects compare and logical operators to combine logicals. Next, you'll use this knowledge to build conditional statements."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Operators: `==`, `!=`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 1,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 1,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 1,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"TRUE"
]
},
"execution_count": 1,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# Comparison of logicals\n",
"TRUE == FALSE\n",
"\n",
"# Comparison of numerics\n",
"-6 * 14 != 17 - 101\n",
"\n",
"# Comparison of character strings\n",
"\"useR\" == \"user\"\n",
"\n",
"# Compare a logical with a numeric\n",
"TRUE == 1"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Operators: `>`, `<`, `>=`, `<=`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 2,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"TRUE"
]
},
"execution_count": 2,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"TRUE"
]
},
"execution_count": 2,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# Comparison of numerics\n",
"-6 * 5 + 2 >= -10 + 1\n",
"\n",
"# Comparison of character strings\n",
"\"raining\" <= \"raining dogs\"\n",
"\n",
"# Comparison of logicals\n",
"TRUE > FALSE"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Imagine we had information on how many people visited our linkedin and facebook profiles in a given week."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"</ol>\n"
]
},
"execution_count": 3,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"</ol>\n"
]
},
"execution_count": 3,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"</ol>\n"
]
},
"execution_count": 3,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# Initiliaze the linkedin and facebook vectors\n",
"linkedin <- c(16, 9, 13, 5, 2, 17, 14)\n",
"facebook <- c(17, 7, 5, 16, 8, 13, 14)\n",
"\n",
"# Popular days\n",
"linkedin > 15\n",
"\n",
"# Quiet days\n",
"linkedin <= 5\n",
"\n",
"# LinkedIn more popular than Facebook\n",
"linkedin > facebook"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<tbody>\n",
"\t<tr><td>FALSE</td><td>FALSE</td><td> TRUE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td></tr>\n",
"\t<tr><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td> TRUE</td><td>FALSE</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"execution_count": 4,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<table>\n",
"<tbody>\n",
"\t<tr><td>FALSE</td><td>TRUE </td><td>TRUE </td><td> TRUE</td><td>TRUE </td><td>FALSE</td><td>TRUE </td></tr>\n",
"\t<tr><td>FALSE</td><td>TRUE </td><td>TRUE </td><td>FALSE</td><td>TRUE </td><td> TRUE</td><td>TRUE </td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"execution_count": 4,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# What about matrices?\n",
"views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)\n",
"\n",
"# When does views equal 13?\n",
"views == 13\n",
"\n",
"# When is views less than or equal to 14?\n",
"views <= 14"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Operators: `|` (or), `%` (and)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"TRUE"
]
},
"execution_count": 5,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 5,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"last <- tail(linkedin, 1)\n",
"\n",
"# Is last under 5 or above 10?\n",
"last < 5 | last > 10\n",
"\n",
"# Is last between 15 (exclusive) and 20 (inclusive)?\n",
"last > 15 & last <= 20"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>FALSE</li>\n",
"</ol>\n"
]
},
"execution_count": 6,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>FALSE</li>\n",
"\t<li>TRUE</li>\n",
"\t<li>TRUE</li>\n",
"</ol>\n"
]
},
"execution_count": 6,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"<table>\n",
"<tbody>\n",
"\t<tr><td>FALSE</td><td>FALSE</td><td> TRUE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>TRUE </td></tr>\n",
"\t<tr><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td><td> TRUE</td><td>TRUE </td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"execution_count": 6,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# linkedin exceeds 10 but facebook below 10\n",
"linkedin > 10 & facebook < 10\n",
"\n",
"# When were one or both visited at least 12 times?\n",
"linkedin >= 12 | facebook >= 12\n",
"\n",
"# When is views between 11 (exclusive) and 14 (inclusive)?\n",
"views > 11 & views <= 14"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Operator: `!` (not)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 7,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 7,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 7,
"metadata": {
},
"output_type": "execute_result"
},
{
"data": {
"text/html": [
"FALSE"
]
},
"execution_count": 7,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"!TRUE\n",
"!(5 > 3)\n",
"!!FALSE\n",
"\n",
"x <- 5\n",
"y <- 7\n",
"!(!(x < 4) & !!!(y > 12))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### If statement\n",
"\n",
"The basic syntax:"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"if (condition) {\n",
" expr\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Showing LinkedIn information\"\n"
]
}
],
"source": [
"# Variables related to your last day of recordings\n",
"medium <- \"LinkedIn\"\n",
"num_views <- 14\n",
"\n",
"# Examine the if statement for medium\n",
"if (medium == \"LinkedIn\") {\n",
" print(\"Showing LinkedIn information\")\n",
"}\n",
"\n",
"# Write the if statement for num_views\n",
"if (num_views > 15) {\n",
" print(\"You're popular!\")\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"if (condition) {\n",
" expr1\n",
"} else {\n",
" expr2\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Showing LinkedIn information\"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Try to be more visible!\"\n"
]
}
],
"source": [
"# Variables related to your last day of recordings\n",
"medium <- \"LinkedIn\"\n",
"num_views <- 14\n",
"\n",
"# Control structure for medium\n",
"if (medium == \"LinkedIn\") {\n",
" print(\"Showing LinkedIn information\")\n",
"} else {\n",
" print(\"Unknown medium\")\n",
"}\n",
"\n",
"# Control structure for num_views\n",
"if (num_views > 15) {\n",
" print(\"You're popular!\")\n",
"} else {\n",
" print(\"Try to be more visible!\")\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"if (condition1) {\n",
" expr1\n",
"} else if (condition2) {\n",
" expr2\n",
"} else if (condition3) {\n",
" expr3\n",
"} else {\n",
" expr4\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Showing LinkedIn information\"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Your number of views is average\"\n"
]
}
],
"source": [
"# Variables related to your last day of recordings\n",
"medium <- \"LinkedIn\"\n",
"num_views <- 14\n",
"\n",
"# Control structure for medium\n",
"if (medium == \"LinkedIn\") {\n",
" print(\"Showing LinkedIn information\")\n",
"} else if (medium == \"Facebook\") {\n",
" # Add code to print correct string when condition is TRUE\n",
" print(\"Showing Facebook information\")\n",
"} else {\n",
" print(\"Unknown medium\")\n",
"}\n",
"\n",
"# Control structure for num_views\n",
"if (num_views > 15) {\n",
" print(\"You're popular!\")\n",
"} else if (num_views <= 15 & num_views > 10) {\n",
" # Add code to print correct string when condition is TRUE\n",
" print(\"Your number of views is average\")\n",
"} else {\n",
" print(\"Try to be more visible!\")\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### While loop\n",
"\n",
"The loop continues to be run until the conditions becomes false. If the condition is never false, the loop runs infinitely!"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"while (condition) {\n",
" expr\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Slow down!\"\n",
"[1] \"Slow down!\"\n",
"[1] \"Slow down!\"\n",
"[1] \"Slow down!\"\n",
"[1] \"Slow down!\"\n"
]
},
{
"data": {
"text/html": [
"29"
]
},
"execution_count": 11,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# Initialize the speed variable\n",
"speed <- 64\n",
"\n",
"# Code the while loop\n",
"while (speed > 30) {\n",
" print(\"Slow down!\")\n",
" speed <- speed - 7\n",
"}\n",
"\n",
"# Print out the speed variable\n",
"speed"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Your speed is 64\"\n",
"[1] \"Slow down big time!\"\n",
"[1] \"Your speed is 53\"\n",
"[1] \"Slow down big time!\"\n",
"[1] \"Your speed is 42\"\n",
"[1] \"Slow down!\"\n",
"[1] \"Your speed is 36\"\n",
"[1] \"Slow down!\"\n"
]
},
{
"data": {
"text/html": [
"30"
]
},
"execution_count": 12,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# Initialize the speed variable\n",
"speed <- 64\n",
"\n",
"# Extend/adapt the while loop\n",
"while (speed > 30) {\n",
" print(paste(\"Your speed is\",speed))\n",
" if (speed > 48) {\n",
" print(\"Slow down big time!\")\n",
" speed <- speed - 11\n",
" } else {\n",
" print(\"Slow down!\")\n",
" speed <- speed - 6\n",
" }\n",
"}\n",
"\n",
"# Print out the speed variable\n",
"speed"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Your speed is 88\"\n"
]
}
],
"source": [
"# Initialize the speed variable\n",
"speed <- 88\n",
"\n",
"while (speed > 30) {\n",
" print(paste(\"Your speed is\", speed))\n",
" \n",
" # Break the while loop when speed exceeds 80\n",
" if (speed > 80) {\n",
" break\n",
" }\n",
" \n",
" if (speed > 48) {\n",
" print(\"Slow down big time!\")\n",
" speed <- speed - 11\n",
" } else {\n",
" print(\"Slow down!\")\n",
" speed <- speed - 6\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### For loop\n",
"\n",
"In a for loop, you iterate over the contents of a vector."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"primes <- c(2, 3, 5, 7, 11, 13)\n",
"\n",
"# loop version 1\n",
"for (p in primes) {\n",
" print(p)\n",
"}\n",
"\n",
"# loop version 2\n",
"for (i in 1:length(primes)) {\n",
" print(primes[i])\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 16\n",
"[1] 9\n",
"[1] 13\n",
"[1] 5\n",
"[1] 2\n",
"[1] 17\n",
"[1] 14\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 16\n",
"[1] 9\n",
"[1] 13\n",
"[1] 5\n",
"[1] 2\n",
"[1] 17\n",
"[1] 14\n"
]
}
],
"source": [
"# The linkedin vector has already been defined for you\n",
"linkedin <- c(16, 9, 13, 5, 2, 17, 14)\n",
"\n",
"# Loop version 1\n",
"for (views in linkedin) {\n",
" print(views)\n",
"}\n",
"\n",
"\n",
"\n",
"# Loop version 2\n",
"for (i in 1:length(linkedin)) {\n",
" print(linkedin[i])\n",
" \n",
"} \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"You can also iterate over the contents of a list."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"primes_list <- list(2, 3, 5, 7, 11, 13)\n",
"\n",
"# loop version 1\n",
"for (p in primes_list) {\n",
" print(p)\n",
"}\n",
"\n",
"# loop version 2\n",
"for (i in 1:length(primes_list)) {\n",
" print(primes_list[[i]])\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 2\n",
"[1] 3\n",
"[1] 5\n",
"[1] 7\n",
"[1] 11\n",
"[1] 13\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 2\n",
"[1] 3\n",
"[1] 5\n",
"[1] 7\n",
"[1] 11\n",
"[1] 13\n"
]
}
],
"source": [
"primes_list <- list(2, 3, 5, 7, 11, 13)\n",
"\n",
"# loop version 1\n",
"for (p in primes_list) {\n",
" print(p)\n",
"}\n",
"\n",
"# loop version 2\n",
"for (i in 1:length(primes_list)) {\n",
" print(primes_list[[i]])\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"You iterate over the contents of a matrix too, but you have to use two nested for loops. Generally, you can iterate over any n-dimensional data structure with n nested for loops."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```\n",
"for (var1 in seq1) {\n",
" for (var2 in seq2) {\n",
" expr\n",
" }\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"On row 1 and column 1 the board contains O\"\n",
"[1] \"On row 1 and column 2 the board contains NA\"\n",
"[1] \"On row 1 and column 3 the board contains X\"\n",
"[1] \"On row 2 and column 1 the board contains NA\"\n",
"[1] \"On row 2 and column 2 the board contains O\"\n",
"[1] \"On row 2 and column 3 the board contains O\"\n",
"[1] \"On row 3 and column 1 the board contains X\"\n",
"[1] \"On row 3 and column 2 the board contains NA\"\n",
"[1] \"On row 3 and column 3 the board contains X\"\n"
]
}
],
"source": [
"ttt <- matrix(c(\"O\", NA, \"X\", NA, \"O\", \"O\", \"X\", NA, \"X\"), byrow=T, nrow=3)\n",
"\n",
"# define the double for loop\n",
"for (i in 1:nrow(ttt)) {\n",
" for (j in 1:ncol(ttt)) {\n",
" print(paste(\"On row\", i, \"and column\", j, \"the board contains\", ttt[i,j]))\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"You're popular!\"\n",
"[1] 16\n",
"[1] \"Be more visible!\"\n",
"[1] 9\n",
"[1] \"You're popular!\"\n",
"[1] 13\n",
"[1] \"Be more visible!\"\n",
"[1] 5\n",
"[1] \"Be more visible!\"\n",
"[1] 2\n",
"[1] \"You're popular!\"\n",
"[1] 17\n",
"[1] \"You're popular!\"\n",
"[1] 14\n"
]
}
],
"source": [
"# The linkedin vector has already been defined for you\n",
"linkedin <- c(16, 9, 13, 5, 2, 17, 14)\n",
"\n",
"# Code the for loop with conditionals\n",
"for (li in linkedin) {\n",
" if (li > 10) {\n",
" print(\"You're popular!\")\n",
" } else {\n",
" print(\"Be more visible!\")\n",
" }\n",
" print(li)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 5\n"
]
}
],
"source": [
"# Pre-defined variables\n",
"rquote <- \"r's internals are irrefutably intriguing\"\n",
"chars <- strsplit(rquote, split = \"\")[[1]] # This splits the string into each of its characters\n",
"\n",
"# Initialize rcount\n",
"rcount <- 0\n",
"\n",
"# Finish the for loop\n",
"for (char in chars) {\n",
" if (char == \"r\") {\n",
" rcount <- rcount + 1\n",
" }\n",
" if (char == \"u\") {\n",
" break\n",
" }\n",
" \n",
"}\n",
"\n",
"# Print out rcount\n",
"print(rcount)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R (R-Project)",
"language": "r",
"name": "ir"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
# 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