[R] return value in function

Liaw, Andy andy_liaw at merck.com
Thu Feb 26 04:25:00 CET 2004


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

> From: li xian
> 
> suppose I have a function example:
>  
> getMatrix <- function(a,b){
>  
>      A1<-diag(1,2,2)
>  
> }
>  
> If I want to get the both the A1 and dim(A1) from the 
> function, Can I do 
>  
> return(A1,dim(A1)) inside the function ? And how can I access 
> A1 and dim(A1) later on? 
>  
>      
> 
> 
> ---------------------------------
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


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




More information about the R-help mailing list