Understanding Arguments in R Programming – A Complete Guide

post

We'll walk you through how to define arguments, use default values, apply the dots (...) argument, and even pass functions as arguments.

What Are Arguments in R?

When you define a function in R, you specify arguments—these are the inputs your function will use.

When calling the function, you don’t always need to name the arguments.

Arguments can have default values, making them optional.

There’s no limit to how many arguments you can include.

Arguments are listed inside the parentheses () after the function keyword, separated by commas.

# Structure function(arg1, arg2 = default_value, ...) 

Adding More Arguments in R

You can include as many arguments as you need. For example, here’s a function to convert numbers into percentage format:

addPercent <- function(x, mult = 100, ...) {  percent <- round(x * mult, ...)  paste(percent, "%", sep = "") } 

Usage:

percentages <- c(58.23, 120.4, 33) addPercent(percentages / 100) 

This function lets you scale numbers and format them with a percentage symbol.

Adding the mult Argument

To add a new argument like mult, simply include it in the function definition:

addPercent <- function(x, mult) {  percent <- round(x * mult, digits = 1)  paste(percent, "%", sep = "") } 

Note:

If you don’t provide a value for mult, R will return an error saying ‘mult is missing’.

Using Default Values in Arguments

To avoid specifying the same value repeatedly, you can assign default values to arguments:

addPercent <- function(x, mult = 100) {  percent <- round(x * mult, digits = 1)  paste(percent, "%", sep = "") } 

Now, calling addPercent(x) without mult will automatically use 100.

The Dots ... Argument

The ... argument allows your function to accept any number of extra parameters, which can then be passed to internal functions like round().

addPercent <- function(x, mult = 100, ...) {  percent <- round(x * mult, ...)  paste(percent, "%", sep = "") } 

Use it with care—if you pass unexpected arguments to functions that don’t expect them, R will throw an error.

Passing Functions as Arguments

You can also pass a function as an argument to another function using a parameter like FUN.

addPercent <- function(x, mult = 100, FUN = round, ...) {  percent <- FUN(x * mult, ...)  paste(percent, "%", sep = "") } 

Now you can choose how values are processed—by default it uses round(), but you can pass any other compatible function.

Anonymous Functions in R

If you need a quick one-time function, use an anonymous function—a function without a name.

profits <- c(2100, 1430, 3580, 5230) addPercent(profits, FUN = function(x) round(x / sum(x) * 100)) 

This allows you to perform inline calculations without writing a separate named function.

Summary

✅ Adding and using function arguments
✅ Assigning default values
✅ Using the flexible ... argument
✅ Passing functions and anonymous functions as arguments

 


Share This Job:

Write A Comment

    No Comments