[R] Object call

Gabor Grothendieck
Mon, 15 May 2006 05:45:22 -0000

Suggest you try to simplify your functions down to the essentials
before posting.  If the problem is how to return multiple variables
use a list:

f <- function(x) list(y = x+1, z = x + 2)

out <- f(1)
out$y
out$z


On 5/15/06, SUMANTA BASAK <r_econometrics at yahoo.co.in>
wrote:
> Hi All,
>
> I have a function like this:
>
> windowlength<-function(x)
> {
>
> mat <- matrix(rnorm(331*12),331,12)
> z <- rep(seq(0,331,by=11)+1, each=2)
> zz <- z[-c(1,length(z))]
> ind <- as.data.frame(matrix(zz, nr=2))
> lapply(ind, function(x) mat[x[1]:x[2],])
>
> cat("For",x/4,"month i.e",x,"week, number of
windows is = ",length(ind),"\n")
>
>
>
> jj<-length(ind)
> beta_val<-matrix(0,jj,ncol(mat))
> prc_chng<-matrix(0,jj,ncol(mat))
>
> p<-x-1
>
> for (j in 2:ncol(mat))
> {
> i<-1
> k<-1
> while (i<=jj)
> {
>
beta_val[i,j]<-cov(mat[(k):(k+p),1],mat[(k):(k+p),j])/sd(mat[(k):(k+p),1])
> prc_chng[i,j]<-((mat[(k+p),j]-mat[(k),j])/mat[(k),j])*100
> k<-k+p
> i<-i+1
> }
> }
> ## NEW MATRIX - JUST DELETE THE FIRST COLUMN ##
> beta_val<-beta_val[,(-1)]
> prc_chng<-prc_chng[,(-1)]
> return(beta_val)
> return(prc_chng)
>
> win.size<-c(12,36,48,96)
>
> for(i in 1:4)
> {
> hh<-windowlength(x=win.size[i])
> return(hh)
> }
>
> }
>
> How can i access the beta_val & prc_chng after running this code? I
returned
> beta_val & prc_chng but after running the function, while i'm
trying to see beta_val & prc_chng, it's giving an error: Error: object
"beta_val" not found.
> Please help me.
>
> Thanks,
> Sumanta Basak.
>
>
>
> ---------------------------------
>  What makes Sachin India's highest paid sports celebrity?, Share your
knowledge on Yahoo! India Answers
>  Send instant messages to your online friends - NOW
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>