- 3 Minutes Wednesdays
- Posts
- 3MW (How to write better and more powerful functions)
3MW (How to write better and more powerful functions)
Guten Tag!
Many greetings from Ulm, Germany.
Powerful functions are your gateway to a happier coding experience. If done right, refactoring your code into separate functions can save you time and nerves.
Today, I'm going to show you two advanced concepts so that your functions can become even better. Specifically, we'll
use arbitrary variables with curly-curly
pass along variables with dot-dot-dot
Use arbitrary variables with curly-curly
Imagine that you want to count variables in your data set. For example, you could take the Palmerpenguins data set and count occurrences for each categorical variable. While you're at it, why not also compute relative counts as well? Here's how that might look.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/dd9e62c6-6f40-4c8c-b59d-726415bb41b3/carbon_61_.png)
Notice how the logic is always the same? The only thing that changes is the variable that we use. So, let's try to pour that logic into a new function. But if we're not careful, things may go awry. Have a look.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/8357a6a1-a705-4ea1-b56f-a61ac599afa2/carbon_65_.png)
Damn, that's annoying. The straight-forward approach of just passing a variable to count() did not work. In this scenario, count() was looking for variables named "variable" instead of "island".
To fix that, you can use the curly-curly operator. It works by wrapping the variable into two-curly brackets (each side). By the power of curly-curly wizardry everything runs smoothly now.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/dbcdb5bb-c9f5-440c-af76-4daaa8777d5c/carbon_67_.png)
Pass along variables with dot-dot-dot
Now, let's take it up a notch. Let's not count one but multiple variables. Super exciting, right? Here's an example that counts sex and species.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/b4fb0c50-2511-4f22-b57f-7dd6c89ef7d8/carbon_68_.png)
We cannot rewrite this into a function using curly-curly. It's designed for one variable only. But we can use dot-dot-dot instead.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/38689ca4-7937-42ab-904f-7b9e61457232/carbon_70_.png)
Marvelous! Basically, dot-dot-dot is R's way of saying "We should take these arguments and push it somewhere else." As you can see, you need to do two things
Add "..." to the function call
Put "..." at the location where all the arguments are supposed to go
Recommended Resources
More on curly-curly and dot-dot-dot: For a more thorough explanation and a somewhat more complex example, check out my blog post "Writing versatile functions with R". Alternatively, you can also check out the corresponding YT video.
R4DS by Hadley Wickham: It's the R data science bible. So you probably know it. Still, it can't hurt to revisit the functions chapter of the book.
Best practices for functions by Cosima Meyer: I know that I have shared this two weeks ago. But it fits quite well here. So let me repeat this resource for our newcomers. Cosima (under the guise of the R-Ladies account) shared a great list of tips for creating functions. Definitely worth a read.
That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.
Enjoy the rest of your day!Albert
Reply