Skip to main content
Frank ZHANG avatar

Frank ZHANG has completed

Writing Functions in R

Start course For Free
4 hours
7,250 XP
Statement of Accomplishment Badge

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

GroupTraining 2 or more people?

Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and more
Try DataCamp for BusinessFor a bespoke solution book a demo.
  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
    Writing a function in R
    50 xp
    Writing a function
    100 xp
    Arguments
    100 xp
    Function output
    50 xp
    Environments
    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
For Business

GroupTraining 2 or more people?

Get your team access to the full DataCamp library, with centralized reporting, assignments, projects and more

Datasets

Swimming Pools

Collaborators

Collaborator's avatar
Nick Carchedi
Collaborator's avatar
Tom Jeon

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

Create Your Free Account

GoogleLinkedInFacebook

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.