Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 47 / 68

Returning several values from a function (lists)

This lesson covers returning several values from one function with a list in R, with the aim of writing functions not limited to a single return value.

list() lets a function return several values as one bundle. It is a light-touch way to hand back related results together, such as a minimum and a maximum.

In the sample code, min_max() returns both with list(min = min(nums), max = max(nums)). The caller reaches each value by name, as result$min and result$max.

A common early stumble is leaving the list elements unnamed, which makes it hard to tell which value is which. Naming them min = ..., max = ... as in the sample makes the code far more readable.

In professional work, lists are used constantly to return several related values together, as statistical functions do.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)