Extracting information from the dataset
1. Extracting information from the dataset
Let's now see how we manipulate functions to clean our list.2. Function manipulation
In this course, we have seen several functions that output another function. A function that behaves as such is called an high-order function. partial() and compose() from purrr have this behavior: they return another function. We have seen them in the previous chapters, and we will be using them again in the following exercises.3. partial()
Remember partial()? partial() is a purrr function used to prefill a function. It takes a function as input and a list of parameters with values. These parameters will be passed to the newly created function, which can be used inside an iteration (or anywhere else).4. compose()
With compose(), you're creating a new function from two or more functions. We've seen this function in the previous chapters. The idea with compose() is to make your code clearer: if you need to call a combination of functions several times, you can write a composed function at the beginning of your script, and call this newly created function every time you need it.5. Cleaner lists
The purrr package also has functions that can be used to clean lists. In the exercises, you will be using compact(), which you have already seen in the previous chapters, and flatten(), a function we haven't seen yet, which is designed to remove one level from a nested list.6. compact()
In Chapter 2 of this course, we've seen compact(). This function removes all the NULL elements from a list. It allows you to have a cleaner version of your results, as you might not need the NULL elements.7. flatten()
flatten() is a function that can be useful when you have a list that is nested and want to remove a level of hierarchy to make it easier to manipulate. For example, if you have a list of lists, you can map over this list to extract an element, and then pass the result to flatten(), so that you only have a list with one level of depth. It's what we are going to do in the exercises.8. Let's practice!
Let's try these functions in the exercises.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.