Writing Functions in R

Learn the fundamentals of writing functions in R so you can make your code more readable and automate repetitive tasks.
Start Course for Free
Clock4 HoursPlay19 VideosCode86 ExercisesGroup66,018 Learners
Database7250 XP

Create Your Free Account

Google LinkedInFacebook
or
By continuing you accept the Terms of Use and Privacy Policy. You also accept that you are aware that your data will be stored outside of the EU and that you are above the age of 16.

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.

  1. 1

    A quick refresher

    Free
    Before 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.
    Play Chapter Now
  2. 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).
    Play Chapter Now
  3. 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).
    Play Chapter Now
  4. 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.
    Play Chapter Now
  5. 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.
    Play Chapter Now
Datasets
Swimming Pools
Collaborators
Nick CarchediTom Jeon
Team RStudio Headshot

Team RStudio

See More

What do other learners have to say?

I've used other sites—Coursera, Udacity, things like that—but DataCamp's been the one that I've stuck with.

Devon Edwards Joseph
Lloyds Banking Group

DataCamp is the top resource I recommend for learning data science.

Louis Maiden
Harvard Business School

DataCamp is by far my favorite website to learn from.

Ronald Bowers
Decision Science Analytics, USAA

Join over 6 million learners and start Writing Functions in R today!

Create Your Free Account

Google LinkedInFacebook
or
By continuing you accept the Terms of Use and Privacy Policy. You also accept that you are aware that your data will be stored outside of the EU and that you are above the age of 16.