Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The logic of passing a missing argument to the R-function rep

Tags:

r

The following R function

tmp <- function(p)
        rep(0, length.out = p)

puzzles me, since

> tmp()
[1] 0

I would expect an error in the call, since p is missing. The documentation for rep says that

Function rep is a primitive, but (partial) matching of argument names is performed as for normal functions. You can no longer pass a missing argument to e.g. length.out.

I don't understand the logic here. Why does rep seem to ignore that p is missing?

R version 3.0.2 (2013-09-25)

like image 690
NRH Avatar asked Jan 27 '14 11:01

NRH


People also ask

What is missing argument to function call R?

The missing argument is an object that triggers an error if and only if it is the result of evaluating a symbol. No error is produced when a function call evaluates to the missing argument object.

Can you pass a function as an argument in R?

14.1 Functions in RFunctions can be passed as arguments to other functions. This is very handy for the various apply functions, like lapply() and sapply() . Functions can be nested, so that you can define a function inside of another function.


1 Answers

In the documentation, I see:

The default behaviour is as if the call was

rep(x, times = 1, length.out = NA, each = 1)

But it does not apply for rep.int, which requires times argument:

>rep(0,)
[1] 0

>rep.int(0,)
Error in rep.int(0, ) : argument "times" is missing, with no default
like image 133
Zbynek Avatar answered Oct 04 '22 20:10

Zbynek