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
4 Hours19 Videos86 Exercises66,020 Learners
7250 XP

Create Your Free Account



By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA. You confirm you are at least 16 years old (13 if you are an authorized Classrooms user).

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


    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
    Writing a function in R
    50 xp
    Writing a function
    100 xp
    100 xp
    Function output
    50 xp
    50 xp
    Testing your understanding of scoping (1)
    50 xp
    Testing your understanding of scoping (2)
    50 xp
    Testing your understanding of scoping (3)
    50 xp
    Data structures
    50 xp
    Atomic types of vectors
    50 xp
    Subsetting lists
    100 xp
    Exploring lists
    100 xp
    for loops
    50 xp
    A safer way to create the sequence
    100 xp
    Keeping output
    100 xp
  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. 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


Swimming Pools


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