Skip to content

return value in function

1 message · Liaw, Andy

#
In general, one would use a list to wrap all objects to be returned into one
object; e.g.,

getMatrix <- function(a, b) {
    A1 <- diag(1,2,2)
    return(list(matrix=A1, dim=dim(A1)))
}

You can then access them as:

mat <- getMatrix(1,1)
mat$matrix
mat$dim

My question is, why do you need to return the dim of the matrix?  The
dimension is already stored in the matrix object as an attribute.  Why store
it yet again?

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}