The power of iteration
1. The power of iteration
Welcome to a course designed to teach you all about the purrr package. purrr allows us to simplify iteration, either with vectors or lists, without having to deal with for loops.2. Iteration without purrr
Iteration lets us do the same thing over and over again with different inputs. This means we don't have to write out repetitive lines of code for each new input. Iteration saves time, reduces lines of code, and prevents typos. For instance, on the left, we can read in files with a separate line for each file. Or we can build a for loop that reads them in iteratively. When there are only three files this might not make sense, but when we are dealing with dozens or hundreds of inputs it can be a huge time saver.3. Iteration without purrr
Here we have an example where we want to add the numbers 0 through 10 together. We can do this manually, or we can write a for loop to do it. For loops are powerful, but are often one of the places where typos that are tough to identify pop up and this prevents you from moving on in your workflow.4. Iteration with purrr
purrr makes iteration easier. It wraps a for loop into a single function, map(), which reduces the number of lines of code we need. This means we can focus on the important pieces of what we are trying to accomplish. Instead of worrying about indexing and brackets or curly braces, we can just put in the pieces and get what we need. map() is very powerful; here we are going to show some relatively simple examples and build complexity throughout the chapters of this course. First, it's important to understand that map() takes two arguments. The first is the dot x argument, which is an object; it can be either a list or a vector. The second argument is the dot f argument, which is a function. The different elements of the dot x argument will be input into the dot f argument for iteration.5. Let's work through an example
In this example, we are going to be working with a list called bird underscore counts, where each element contains a different string of numbers. Each element represents one park, and the counts are the number of birds migrating over the park on a given day in the month of September. Different parks counted birds for different numbers of days, so they have different lengths.6. Example time!
If we want to determine the sum of the numbers in each element we can do it two ways. First, we can use four lines of R code including a for loop to iterate over the elements of bird_counts, putting each element into the sum() function and then putting the result into a new list, called bird_sum. Or, we can use one line of purrr code to calculate the sum of each element of the bird_counts list and put it into the new bird_sum list. The output of both methods is the same! However, purrr makes our code simpler by letting us focus on the pieces we are working with, instead of the structure of the for loop.7. Let's purrr-actice!
Now let's get started with some examples.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.