[R] return value in function

Jason Turner jasont at indigoindustrial.co.nz
Thu Feb 26 04:21:19 CET 2004


> 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?

The general approach for this is to use a list

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

foo <- getMatrix(something)
foo$diag
foo$dim

Cheers

Jason




More information about the R-help mailing list