The humble ellipsis or three dots in R functions
SN248 / 2017-11-12
So, I was writing a function and within that function, I needed to call the mean
function.
Of course, mean
can take many arguments (see ?mean
) and one of them is na.rm
. I didn’t want to have na.rm
as a required input in my function. I found that the way to have additional variable number of inputs in an r
function is the ellipsis or three-dots operator.
A very trivial implementation is as follows:
Additionally, the optional arguments provided can be accessed via a list, using
mylist <- list(...)
.
This post is based on the excellent article here - http://www.burns-stat.com/the-three-dots-construct-in-r/
Just summarizing the main points of the article above, the main reason for using ellipsis is to have variable number of arguments in a function, and to be able to pass those arguments to another function within the first function.
One more thing to note, there can be only one set of ellipsis in the function arguments, and ellipsis should be at the very end (atleast at the end of named arguments) in the function signature.