Loved by learners at thousands of companies
Course Description
Functions are a fundamental building block of the R language. You've probably used dozens (or even hundreds) of functions written by others, but in order to take your R game to the next level, you'll need to learn to write your own functions. This course will teach you the fundamentals of writing functions in R so that, among other things, you can make your code more readable, avoid coding errors, and automate repetitive tasks.
For Business
Training 2 or more people?
Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and more- 1
A quick refresher
FreeBefore we embark, we'll make sure you’re ready for this course by reviewing some of the prerequisites. You'll review the syntax of writing a function in R, the basic data types in R, subsetting and writing for loops. It won't all be review, we'll introduce you to a few new things that will be helpful throughout the course.
Writing a function in R50 xpWriting a function100 xpArguments100 xpFunction output50 xpEnvironments50 xpTesting your understanding of scoping (1)50 xpTesting your understanding of scoping (2)50 xpTesting your understanding of scoping (3)50 xpData structures50 xpAtomic types of vectors50 xpSubsetting lists100 xpExploring lists100 xpfor loops50 xpA safer way to create the sequence100 xpKeeping output100 xp - 2
When and how you should write a function
Writing your own functions is one way to reduce duplication in your code. In this chapter, you'll learn when to write a function, how to get started and what to keep in mind when you are writing. You'll also learn to appreciate that functions have two audiences: the computer (which runs the code) and humans (who need to be able to understand the code).
Why should you write a function?50 xpStart with a snippet of code100 xpRewrite for clarity100 xpFinally turn it into a function!100 xpHow should you write a function?50 xpStart with a simple problem100 xpRewrite snippet as function100 xpPut our function to use100 xpHow can you write a good function?50 xpGood function names50 xpArgument names100 xpArgument order100 xpReturn statements100 xpWhat does this function do?100 xpLet's make it clear from its name100 xpMake the body more understandable100 xpMuch better! But a few more tweaks...100 xp - 3
Functional programming
You already know how to use a for loop. The goal of this chapter is to teach you how to use the map functions in the purrr package which remove the code that's duplicated across multiple for loops. After completing this chapter you'll be able to solve new iteration problems with greater ease (faster and with fewer bugs).
Why functional programming?50 xpUsing a for loop to remove duplication100 xpTurning the for loop into a function100 xpWhat about column means?100 xpWhat about column standard deviations?100 xpUh oh...time to write a function again100 xpFunctions can be arguments too50 xpUsing a function as an argument100 xpIntroducing purrr50 xpThe map functions100 xpThe ... argument to the map functions100 xpPicking the right map function100 xpShortcuts50 xpSolve a simple problem first100 xpUsing an anonymous function100 xpUsing a formula100 xpUsing a string100 xpUsing a numeric vector100 xpPutting it together with pipes100 xp - 4
Advanced inputs and outputs
Now you've seen how useful the map functions are for reducing duplication, we'll introduce you to a few more functions in purrr that allow you to handle more complicated inputs and outputs. In particular, you'll learn how to deal with functions that might return an error, how to iterate over multiple arguments and how to iterate over functions that have no output at all.
Dealing with failure50 xpCreating a safe function100 xpUsing map safely100 xpWorking with safe output100 xpWorking with errors and results100 xpMaps over multiple arguments50 xpGetting started100 xpMapping over two arguments100 xpMapping over more than two arguments100 xpArgument matching100 xpMapping over functions and their arguments100 xpMaps with side effects50 xpWalk100 xpWalking over two or more arguments100 xpPutting together writing functions and walk100 xpNice breaks for all100 xpWalking with many arguments: pwalk100 xpWalking with pipes100 xp - 5
Robust functions
In this chapter we'll focus on writing functions that don't surprise you or your users. We'll expose you to some functions that work 95% of the time, and 5% of the time fail in surprising ways. You'll learn which functions you should avoid using inside a function and which you should use with care.
Robust functions50 xpAn error is better than a surprise100 xpAn informative error is even better100 xpA different kind of surprise: side effects50 xpUnstable types50 xpsapply is another common culprit50 xpUsing purrr solves the problem100 xpA type consistent solution100 xpOr fail early if something goes wrong100 xpNon-standard evaluation50 xpProgramming with NSE functions100 xpWhen things go wrong100 xpWhat to do?100 xpHidden arguments50 xpA hidden dependence100 xpLegitimate use of options100 xpWrap-up50 xp
For Business
Training 2 or more people?
Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and moreJoin over 13 million learners and start Writing Functions in R today!
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.