<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-primer-in-functional-programming-in-r-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/01/a-primer-in-functional-programming-in-r-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=a-primer-in-functional-programming-in-r-part-2"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/learn-2300141__340.jpg" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/learn-2300141__340-150x150.jpg?resize=150%2C150" alt="" class="wp-image-6027" data-recalc-dims="1"></a></p>
<p>In the last <a rel="nofollow" href="http://www.r-exercises.com/2017/05/19/a-primer-in-functional-programming-in-r/" target="_blank">exercise</a>, We have seen how powerful functional programming principles can be and how it can drammatically increase the readablity of the code and how easily you can work with them .In this set of exercises we will look at functional programming principles with <code>purrr</code>.Purrr comes with a number of interesting features and is really useful in writing clean and concise code . Please check the documentation and load the purrr library in your R session before starting these exercise set .<br>
Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/01/solutions-a-primer-in-functional-programming-in-r-part-2/" target="_blank">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>
From the airquality dataset( available in base R ) , Find the mean ,median ,standard deviation of all columns using map functions . </p>
<p><strong>Exercise 2</strong></p>
<p>In the same dataset,find 95th percentile of each column excluding the NA values </p>
<p><strong>Exercise 3</strong><br>
Load the iris dataset ,with help of <code>pipe</code> and <code>map </code> functions find out the mean of the relavant columns.Keep in mind mean is meant for numeric columns ,so you may need multiple map like functions.I expect the output as a dataframe . </p>
<p><strong>Exercise 4</strong><br>
I have a vector <code> x &lt;- 1:20 </code>,I want to multiply every odd element with 2 and every even element with 4 , find a solution using purrr . </p>
<p><strong>Exercise 5</strong><br>
I have a sequence <code> x &lt;- 1:100</code> , I want to increase the 1st,11th, 21st…91st element by 1 . How can I achieve that .<br>
<strong>Exercise 6</strong><br>
Suppose I have two character vectors<br>
x &lt;-letters # letters is a vector of alphabets in small available in R<br>
y &lt;-LETTERS # LETTERS is a vector of alphabets in caps available in R</p>
<p>How do I join each capital letter with the small letter so that the end result looks like this<br>
&quot;A,a&quot; &quot;B,b&quot; ,.. and so on </p>
<p><strong>Exercise 7</strong><br>
The previous exercise gave you the intuition of how to work parallely on two vectors.Now accepts a list of character vectors of same size and joins them like the previous exercise . so if I have a<br>
list like <code> x &lt;- list(c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;),c(&quot;d&quot;,&quot;e&quot;,f&quot;),c(&quot;g&quot;,&quot;h&quot;,&quot;i&quot;))</code> ,it should give me output as .<br>
<code> [1] &quot;a d g&quot; &quot;b e h&quot; &quot;c f i&quot; </code></p>
<p><strong>Exercise 8</strong><br>
using a functional tool from purrr ,reverse the letters so that my output is a string of 26 character starting from z and ending at a<br>
like “z y x w v u t s r q p o n m l k j i h g f e d c b a”</p>
<p><strong>Exercise 9</strong><br>
Exercise on Purrr wont be complete if We dont mention its “Filter” like functions . take a numeric vector of 1:100 , keep all the numbers which are divisible by 2 and after that remove the one’s which are divisible by 5</p>
<p><strong>Exercise 10</strong><br>
Ability to create partial functions is a powerful tool in functional programming. This allow functions to behave a little like data structures .Consider creating a partial function whenever you see you are repeating functions argument. This may not sound very useful in context of this exercise but I am sure you will find it very useful in your R gigs .Now create a Partial function<br>
ql ,which is to find the 25th percentile and 50th percentile of a column from a dataframe .use it to find the same from airquality dataset .Dont use quantile twice in this exercise .</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/08/05/higher-order-functions-exercises/" title="Higher Order Functions Exercises" target="_blank">Higher Order Functions Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/19/a-primer-in-functional-programming-in-r/" title="A Primer in Functional Programming in R (Part – 1)" target="_blank">A Primer in Functional Programming in R (Part – 1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/" title="Building Shiny App exercises part 4" target="_blank">Building Shiny App exercises part 4 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/01/a-primer-in-functional-programming-in-r-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=a-primer-in-functional-programming-in-r-part-2"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/HHrJzs1ZacI" height="1"></a></div>