**R-exercises**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 .

Answers to the exercises are available here.

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.

**Exercise 1**

create a function which calculates factorial of a number with help of `Reduce `

,

**Exercise 2**

create the same function which calculates factorial but with recursion and memoization. `:`

**Exercise 3**

create a function ` cum_add`

which makes cumulative summation for e.g if `x <- 1:3 `

cum_add(x) will result in `1 3 6 `

.Don’t use cumsum .

**Exercise 4**

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 ` your_func(iris)`

.Try to avoid loops ,its possible to make it in one line .swag your functional skills .

**Learn more**about Functions in the online course R Programming: Advanced Analytics In R For Data Science. 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!

**Exercise 5**

create a function `centerColumnAroundMean`

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

` df `

then ` centerColumnAroundMean(df) `

will result in

It is possible to achieve this by single line of R , Please dont use loops .

**Exercise 6**

I have list of movies ,which have two movie franchieses as the elements starwars and LOTR,you can create the movie list by

`my_movie_list<- list(STARWARS= list("A NEW HOPE","The Last Jedi","The Force Awakens"),LOTR=list("THE FELLOWSHIP OF THE RING","THE Two Towers","The RETURN of the KING","Hobbit" = list("An unexpected Journey","The Battle of the FIVE ARMY","The Desolation of Smaug") )) `

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

“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 .

**Exercise 7**

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.

**Exercise 8**

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 `table`

**Exercise 9**

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 .

**Exercise 10**

Create a new environment with ` new.env() `

command and create 3 vector variables under that environment as ` a=1:10;b=100:500;c=1000:1500 `

without knowing or manually calling mean for all the vector variables can you print the mean of all variables of the new environment .

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.