Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function values without default that are ignored

Tags:

function

r

We have a function that requires multiple arguments without default values for any of them. However, even if some of them are not specified, the function returns a value, if these parameters are only used to subset a matrix (and possibly other types). We are puzzled why this is the case - can anybody help?

Specifically, why does the following code not return an error, but sums over the entire matrix and ignores j:

foo <- function(mat, j){
  v <- sum(mat[,j])
  v
}

foo(mat = matrix(1:4,3,4))
like image 465
P Block Avatar asked Aug 09 '16 10:08

P Block


2 Answers

According to this blog, optional arguments without defaults are missing from the inside of the function. Testing for it returns TRUE.

foo <- function(mat, j){
   v <- sum(mat[,j]); print(missing(j))
   v
}
foo(mat = matrix(1:4,3,4))
[1] TRUE
[1] 30

Without knowing what the specifics were, I experimented with sum a bit and this is what it shows.

sum(matrix(1:4,3,4)[,NA])
[1] NA
sum(matrix(1:4,3,4)[,NULL])
[1] 0
sum(matrix(1:4,3,4)[,])
[1] 30

If we do not specify any index for the matrix, sum sums all its values.

From reading the blog and the little experiment, I think that your custom functions work in cases where the used function processes provided data and is able to operate with missing arguments. In case of subsetting a matrix, the behaviour with missing arguments for subsets is that the function performs an operation over the whole dataset.

like image 71
nya Avatar answered Sep 21 '22 13:09

nya


My guess is the argument is never evaluated.

foo <- function(x)
    bar(x)

bar <- function(y)
    missing(y)

foo()
#[1] TRUE
foo(43)
#[1] FALSE

The inner function, in your case [, which has formal arguments i, j, ..., drop = FALSE, most likely checks whether the argument j is missing, and if it is missing it doesn't try to evaluate it.

like image 37
Ernest A Avatar answered Sep 22 '22 13:09

Ernest A