In the last exercise, 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 `purrr`

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

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**

From the airquality dataset( available in base R ) , Find the mean ,median ,standard deviation of all columns using map functions .

**Exercise 2**

In the same dataset,find 95th percentile of each column excluding the NA values

**Exercise 3**

Load the iris dataset ,with help of `pipe`

and `map `

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 .

**Exercise 4**

I have a vector ` x <- 1:20 `

,I want to multiply every odd element with 2 and every even element with 4 , find a solution using purrr .

**Exercise 5**

I have a sequence ` x <- 1:100`

, I want to increase the 1st,11th, 21st…91st element by 1 . How can I achieve that .

**Exercise 6**

Suppose I have two character vectors

x <- letters # letters is a vector of alphabets in small available in R

y <- LETTERS # LETTERS is a vector of alphabets in caps available in R

How do I join each capital letter with the small letter so that the end result looks like this

"A,a" "B,b" ,.. and so on

**Exercise 7**

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

list like ` x <- list(c("a","b","c"),c("d","e",f"),c("g","h","i"))`

,it should give me output as .

` [1] "a d g" "b e h" "c f i" `

**Exercise 8**

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

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”

**Exercise 9**

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

**Exercise 10**

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

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 .

## Leave a Reply