<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/a-primer-in-functional-programming-in-r-exercises-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/19/a-primer-in-functional-programming-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=a-primer-in-functional-programming-in-r"> 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" rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/learn-2300141__340.jpg"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-6027" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/learn-2300141__340-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a></p>
<p>In the exercises below we cover the basics of functional programming in R( part 1 of a two series exercises on functional programming) . We consider recursion with R , apply family of functions , higher order functions such as Map ,Reduce,Filter in R .<br>
Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/05/19/solutions-a-primer-in-functional-programming-in-r/">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>
create a function which calculates factorial of a number with help of <code>Reduce </code> , </p>
<p><strong>Exercise 2</strong><br>
create the same function which calculates factorial but with recursion and memoization. <code></code>:</p>
<p><strong>Exercise 3</strong><br>
create a function <code> cum_add</code> which makes cumulative summation for e.g if <code>x &lt;- 1:3 </code>cum_add(x) will result in <code>1 3 6 </code> .Don’t use cumsum .</p>
<p><strong>Exercise 4</strong><br>
create a function which takes a dataframe and returns mean , minimum and maximum of all numeric columns . Your function should take a dataframe as input .for e.g <code> your_func(iris)<br>
</code>.Try to avoid loops ,its possible to make it in one line .swag your functional skills . </p>
<div>
<aside><img src="http://www.r-exercises.com/2017/05/19/a-primer-in-functional-programming-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=a-primer-in-functional-programming-in-r"></aside>
<div><strong>Learn more</strong> about Functions in the online course <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/product/r-programming-advanced-analytics-in-r-for-data-science/">R Programming: Advanced Analytics In R For Data Science</a>. In this course you will learn how to prepare your data quicker and more efficiently, leaving you more time to focus on analyzing and visualizing your results!</div>
</div>
<p><strong>Exercise 5</strong><br>
create a function <code>centerColumnAroundMean</code> which takes a numeric dataframe and manipulates the dataframe such a way that all column’s values are centered with mean value of the column . For example if my data frame is like this<br>
<code> df </code> </p>
<p>then <code> centerColumnAroundMean(df) </code> will result in </p>
<p>It is possible to achieve this by single line of R , Please dont use loops .<br>
<strong>Exercise 6</strong><br>
I have list of movies ,which have two movie franchieses as the elements starwars and LOTR,you can create the movie list by<br>
<code>my_movie_list&lt;-  list(STARWARS= list(&quot;A NEW HOPE&quot;,&quot;The Last Jedi&quot;,&quot;The Force Awakens&quot;),LOTR=list(&quot;THE FELLOWSHIP OF THE RING&quot;,&quot;THE Two Towers&quot;,&quot;The RETURN of the KING&quot;,&quot;Hobbit&quot; = list(&quot;An unexpected Journey&quot;,&quot;The Battle of the FIVE ARMY&quot;,&quot;The Desolation of Smaug&quot;) )) </code><br>
now the problem I am facing is some of the texts are in caps and some of them are in small without any particular order , I would like the list to have a format like<br>
“The Last Jedi” , Help me in writing a function which will do the same . Please keep in mind that the list is a nested list .</p>
<p><strong>Exercise 7</strong><br>
load the diamonds data set from ggplot 2 package , I want to buy a diamond of each color but don’t want to pay the top price , I assume the second highest price is good enough for me . Help me in finding the second highest price for each color from the dataset. </p>
<p><strong>Exercise 8</strong><br>
use the already loaded diamonds dataset from previous exercise , I want to know the average price for each combination of cut and color .your output should be similar to following. Don’t use <code>table</code></p>
<p><strong>Exercise 9</strong><br>
load iris dataset , I want to get the third row for each group of species . Help me to write a short function for me to achieve that . </p>
<p><strong>Exercise 10</strong><br>
Create a new environment with <code>  new.env() </code> command and create 3 vector variables under that environment as <code> a=1:10;b=100:500;c=1000:1500 </code><br>
without knowing or manually calling mean for all the vector variables can you print the mean of all variables of the new environment . </p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Accessing Dataframe Objects Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/05/20/accessing-dataframe-objects-exercises/">Accessing Dataframe Objects Exercises </a></li>
<li><a title="Correlation and Correlogram Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/08/correlation-and-correlogram-exercises/">Correlation and Correlogram Exercises </a></li>
<li><a title="Building Shiny App exercises part 4" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/">Building Shiny App exercises part 4 </a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/r-courses/">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/05/19/a-primer-in-functional-programming-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=a-primer-in-functional-programming-in-r"> 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ITWJpPZTgWM"></a></div>