<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/higher-order-functions-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/higher-order-functions-exercises/" data-counturl="https://www.r-bloggers.com/higher-order-functions-exercises/" data-count="horizontal" data-text="Higher Order Functions Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/08/05/higher-order-functions-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/08/Functions.png?931d95"><img sizes="(max-width: 300px) 100vw, 300px" srcset="http://r-exercises.com/wp-content/uploads/2016/08/Functions-300x148.png 300w, http://r-exercises.com/wp-content/uploads/2016/08/Functions.png 479w" alt="Functions" class="wp-image-1078" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2016/08/Functions-300x148.png?resize=300%2C148" data-recalc-dims="1"></a>Higher order functions are functions that take other functions as arguments or return functions as their result. In this set of exercises we will focus on the former. R has a set of built-in higher order functions: <code>Map</code>, <code>Reduce</code>, <code>Filter</code>, <code>Find</code>, <code>Position</code>, <code>Negate</code>. They enable us to complete complex operations by using simple single-purpose functions as their building blocks. In R this is especially helpful in cases where we cannot depend on vectorization and have to utilize control statements like for loops. In such scenarios higher order functions help us by: a) simplifying and shortening the syntax, b) getting rid of counter indices and c) getting rid of temporary storage values.</p>
<p>Exercises in this section will have to be solved by using one or more of the higher order functions mentioned above. It might be useful reading their help page before continuing.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/08/05/higher-order-functions-solutions/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
You are working on 3 datasets all at once:<br>
<code>multidata &lt;- list(mtcars, USArrests, rock)</code><br>
<code>summary(multidata[[1]])</code> will return the summary information for a single dataset.<br>
Obtain summary information for every dataset in the list.</p>
<p><strong>Exercise 2</strong><br>
<code>cumsum(1:100)</code> returns the cumulative sums of a vector of numbers from 1 to 100.<br>
Do the same using <code>sum</code> and an appropriate higher order function.</p>
<p><strong>Exercise 3</strong><br>
You have a vector of numbers from 1 to 10. You want to multiply all those numbers first by 2 and then by 4. Why the following line does not work and how to fix it?<br>
<code>Map(`*`, 1:10, c(2,4))</code></p>
<p><strong>Exercise 4</strong><br>
Expression <code>sample(LETTERS, 5, replace=TRUE)</code> obtains 5 random letters.<br>
Generate a list with 10 elements, where first element contains 1 random letter, second element 2 random letters and so on.</p>
<p>Note: use a fixed random seed: <code>set.seed(14)</code></p>
<p><strong>Exercise 5</strong><br>
Library <code>spatstat</code> has a function <code>is.prime()</code> that checks if a given number is a prime.<br>
Find all prime numbers between 100 and 200.</p>
<p><strong>Exercise 6</strong><br>
We have a vector containing all the words of the English language –<br>
<code>words &lt;- scan(&quot;http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt&quot;, what=&quot;character&quot;)</code><br>
a. Using a function that checks if a given words contains any vowels:<br>
<code>containsVowel &lt;- function(x) grepl(&quot;a|o|e|i|u&quot;, x)</code><br>
find all words that do not contain any vowels.<br>
b. Using a function <code>is.colour()</code> from the <code>spatstat</code> library find the index of the first word inside the words vector corresponding to a valid R color.</p>
<p><strong>Exercise 7</strong><br>
a. Find the smallest number between 10000 and 20000 that is divisible by 1234.<br>
b. Find the largest number between 10000 and 20000 that is divisible by 1234.</p>
<p><strong>Exercise 8</strong><br>
Consider the <code>babynames</code> dataset from the <code>babynames</code> library.<br>
Start with a list containing the used names for each year:<br>
<code>library(babynames); namesData &lt;- split(babynames$name, babynames$year)</code><br>
a. Obtain a set of names that were present in every year.<br>
b. Obtain a set of names that are only present in year 2014</p>
<p><strong>Exercise 9</strong><br>
Using the same <code>babynames</code> dataset and a function that checks if word has more than 3 letters: <code>moreThan3 &lt;- function(x) nchar(x) > 3</code><br>
Inside each year list leave only the names that have 3 letters or less.</p>
<p><strong>Exercise 10</strong><br>
Using the same <code>babynames</code> dataset:<br>
a. Split each name to a list of letters.<br>
b. Join each list of letters by inserting an underscore “_” after each letter.</p>
<p>Note: if you have a word <code>x &lt;- &quot;exercise&quot;</code> you can split it with <code>x2 &lt;- strsplit(x, &quot;&quot;)</code><br>
and join using underscores with <code>paste(x2[[1]], collapse=&quot;_&quot;)</code></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/higher-order-functions-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/higher-order-functions-exercises/" data-counturl="https://www.r-bloggers.com/higher-order-functions-exercises/" data-count="vertical" data-text="Higher Order Functions Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/08/05/higher-order-functions-exercises/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/WPtzWpR6cMI"></a></div>