Introduction to Writing Functions in R
Learn to turn R scripts into reusable functions, set argument defaults, check inputs, return results and apply it all to a grain yield case study.
Writing functions in R lets an analyst package repeated script code under one name, so the work is easier to read, carries fewer errors and can move between projects. You decide which inputs a function takes and in what order, give them defaults, check what callers pass in, and choose what comes back. Later lessons look at how R finds a variable inside a function, and the final exercises apply these habits to agricultural grain yield data.
What You Will Achieve
- Turn a block of repeated R script code into a function with clearly ordered inputs
- Give arguments sensible defaults and stop a function with an informative error when inputs are wrong
- Return several results from one function and predict which variable R will use inside it
- Convert, plot and model a real dataset using functions you wrote yourself
Program Overview
Writing Functions and Their Arguments
Starts with why functions pay off and turns scripts into functions such as a coin toss, then sets argument defaults and checks.
- Converting a script into a function
- Ordering data and detail arguments
- Numeric, logical, NULL and categorical defaults
- Passing, checking and rejecting bad arguments
Return Values, Environments, and Scope
Looks at what a function hands back and how R decides which variables a function can see.
- Early and invisible returns
- Returning multiple values and metadata
- Creating and exploring environments
- Scope and variable precedence
Grain Yields Case Study
Applies the earlier chapters to grain yield data through data preparation, plotting and modeling steps.
- Converting areas and yields to metric
- Plotting yields over time by region
- Running a model and making yield predictions
Certificate Awarded (Paid)
Job Outlook
Function writing in R is used wherever the same cleaning, plotting or modeling steps are repeated across datasets, as in data analysis, research and reporting work carried out in R.
Good for: R users who repeat the same analysis steps across projects and want to package that work as their own well-behaved functions.
Keep in mind: Practice centers on short exercises and one grain yield case study; applying the functions to your own data is left to you.
Bottom line: A focused grounding in R function design that gives real attention to argument checks, return values and variable scope.
Frequently Asked Questions
What data do the R exercises draw on?
The exercises draw on data about Wyoming tourism, stock price-to-earnings ratios and grain yields. The grain yield data carries the final chapter, where you convert units to metric, plot yields over time by region and fit a model.
How does the course handle variable scope in R?
One chapter looks at environments: how to create and explore them, how to check whether a variable exists, and which value R picks when the same name is defined both inside and outside a function.
What kinds of argument defaults are covered?
The lessons work through numeric, logical, NULL and categorical defaults, then show how to pass arguments on to other functions, including through the ... argument, and how to deal with missing values along the way.


There are no reviews yet.