1. Rlang-ing in your rocking chair
Why did we learn about enquo and bang-bang if they could just be combined together into curly-curly? In this lesson, we'll see how they are used apart by expanding on the function we've been working with.
2. Strange summarize column name
Recall that we left off by not including a name in the summarize function of the grouped_mean_for_column function. This returns that sort of strange column name in backticks. While this does convey what the calculation corresponds to, it will make further analysis complicated because parentheses aren't allowed in R names, so the backticks will be required.
3. What we'd like it to be
What we might want instead is to use underscores and add the text mean_of_ to the front of the column name. Let's see how we can do this with rlang next. We'll need two more helper rlang pieces to get us there.
4. as_name() and the walrus operator :=
We saw that a variable name that isn't quoted will give an error if it isn't first defused with enquo.
The as_name function converts the defused column name into a string that will act as the name of a column.
After defining this column name, we'd like that to be the name of the column in mutate. Unfortunately, R is strict about the kind of expressions supported on the left-hand side of the equals sign. Thus, the rlang package includes the colon-equals operator, better known as the walrus operator. It has this name because it looks like a walrus's face laying down. Let's see these in action next.
5. Wrapping up the function
Let's go one step further here and also specify the tibble as an argument to the function with dot-data. So far, we've only been using the joined tibble, but we could generalize our function to work with other tibbles too.
Next, we defuse the col_to_mean argument with enquo, so we don't get an object error, and then apply as_name to convert the column name into a string.
Then we prepend mean_of_ to the front of our column name text using the paste0 function.
Now we use dot-data instead of joined so we could have a more generalized solution that works on a different tibble with a different grouping variable and a different column to summarize.
To assign a name programmatically to a summarized column, we use bang-bang before new_col_name to change the text to be unquoted. The walrus operator is required to do this assignment with a bang-bang on the left, instead of the ordinary equals sign used in summarize without tidy evaluation. The right of the walrus operator is the mean of our forced and defused column, col_to_mean.
6. Applying the wrapped up function
Let's see how the rural population varies, on average, across the continents for the years in our data. We specify the dot-data argument as joined, the grouping column as continent, and the column to take the mean of as perc_rural_pop. Africa has the largest mean percentage of rural population followed by Asia and then Europe.
7. Cleaning things up a bit
We could leave the bang-bang-enquos in our code. Just to reinforce our understanding, let's replace them with curly-curlys. It looks a little cleaner and more readable, and we also see the code where enquo and bang-bang work without each other.
Lastly, when we call our function, we can pipe joined in, as we specified dot-data as the first argument.
8. Let's practice!
Try out the walrus operator with as_name to build function writing prowess.