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 few tool kits for functional programming in R (with famous `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**

Load the `purrr`

package. For each column in the `mtcars`

data set, calculate the mean. Results should be a list.

**Exercise 2**

Do the same thing as above, but returning the named vector as a result.

**Exercise 3**

Calculate the mean once again, but with max/min 10% values trimmed.

**Exercise 4**

Split `mtcars`

by `cyl`

into a list and calculate the number of rows for each element of the list.

**Exercise 5**

For each element of lists from Exercise 4, calculate the mean of each column. Return results as a `data.frame`

with one record for each input element (3 rows in total).

**Exercise 6**

For each element of the list from Exercise 4, fit a linear model between 1/4 mile time and gross horsepower.

**Exercise 7**

For each model from the previous exercise, extract the co-efficient table.

**Exercise 8**

Fit each of the models to all `mtcars`

.

**Exercise 9**

For each model, plot separate histograms of the predicted value with the model indicated in the title.

**Exercise 10**

For each car, calculate the average prediction of the models.

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

## Leave a Reply