[R] Return od functions

Rui Barradas ruipbarradas at sapo.pt
Sun Jul 29 12:10:03 CEST 2012


Hello,

return( list(y, x, z, dy, dx) )
# or
return( list(y=y, x=x, z=z, dy=dy, dx=dx) )

That's ONE variable returned, a list. In the first case you access the 
list members by number,

ret <- dgp.par(...)
ret[[ 1 ]]   # 'y' value

In the second case, by number or name.

ret[[ 4 ]]
ret[[ "dy" ]]
ret$dy

See An Introduction to R, Chapters 6 and 10, file R-intro.pdf in your 
doc directory.

Hope this helps,

Rui Barradas

Em 29-07-2012 08:24, wangchi3 escreveu:
> Hi!
> I have some questions about R function. I try to write a function for
> multi-returns. The function code is as attachment.
>
> dgp.par<-function(ai, bi, t, n)
> {
> t0<-t+20
> y0<-matrix(0, nr=t0, nc=n)
> y0[1,]<-ai/(1-bi)
>   
> for(tt in 2:t0)
> { y0[tt,]<-ai+bi*y0[tt-1,]+rnorm(n, 0, 1) }
> y<-y0[21:t0,]
> x<-y0[20:t0-1,]
> z<-y0[19:t0-2,]
> z<-z[2:t,]
> dy<-y[2:t,]-y[1:t-1,]
> dx<-x[2:t,]-x[1:t-1,]
> return(y, x, z, dy, dx)
> }
>
> When I execute the function I wrote, it always appears an error message that
> function cannot have multi-returns. Whether the R functions only allow
> single return? If the R function can allow multi-returns, how do I rewrite
> my R function to allow multi-returns?
>
> Thanks a lot.
>
> Eric Wang
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Return-od-functions-tp4638247.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list