One of R`s cool features is functional programming. It makes development much easier and the code you write shorter and less prone to errors. There are a few tool kits for functional programming in R (with famous `base`

`s `apply`

functions family among them). In this set of exercises,you will familiarize yourself with basic functions from the `purrr`

package, which has a great advantage of being consistent both internally and with the rest of the `tidyverse`

packages.

Answers to the exercises are available here.

Please, do all exercises using the `purrr`

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

Calculate the factorial for numbers from 1 to 10.

**Exercise 2**

Create a `data.frame`

with cross products of letters from a to z (first column) and numbers from 1 to 10 (second column).

**Exercise 3**

For list `list1 = list('a', data.frame(1:10), TRUE, 1, letters, 153)`

find the position of the first element of the list that is numeric.

**Exercise 4**

For `list1`

, check if every element is of an atomic type.

**Exercise 5**

Select only atomic elements of `list1`

.

**Exercise 6**

Check if the letter ‘b’ is an element of `list1`

.

**Exercise 7**

Calculate `log`

for each element of `list1`

and handle errors.

**Exercise 8**

Run each function from the list `list2 = list(rnorm, runif, rcauchy)`

to generate 10 samples.

**Exercise 9**

Generate 5 series of 10 samples each from uniform distribution `(0,1)`

.

**Exercise 10**

Combine the list of series from exercise 9 into a single vector.

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

liz says

i don’t know how to do that,who can support an answer.

woodspock says

There’s an error with the link to solutions page.

Right one is

https://www.r-exercises.com/2018/01/19/functional-programming-with-purrr-solutions-part-2/

(19 instead of 13)