Andrew Cox
2005-Dec-20 07:39 UTC
[R] Outputing dataframe or vector from within a user defined function
Hi, I have written a user defined function that carries out a monte carlo simulation and outputs various stats. I would like to access and store the simulation data (a two column local variable) from outside the function I have written. How can I output the data from the function as new variable, accessible outside the function? I am probably going to find that this is really simple, yet I have not been able to find the answer in (usual places, manuals, 2 x Books and mailing lists) Many Thanks Andy Cox
Andrew Cox
2005-Dec-20 08:05 UTC
[R] Outputing dataframe or vector from within a user defined function
Hi, I have written a user defined function that carries out a monte carlo simulation and outputs various stats. I would like to access and store the simulation data (a two column local variable) from outside the function I have written. How can I output the data from the function as new variable(not simply print it), accessible outside the function? I am probably going to find that this is really simple, yet I have not been able to find the answer in (usual places, manuals, 2 x Books and mailing lists) Many Thanks Andy Cox
Petr Pikal
2005-Dec-20 08:35 UTC
[R] Outputing dataframe or vector from within a user defined function
Hi having function e.g. mean.int<-function(x,p=.95) {x.na<-na.omit(x) mu<-mean(x.na) odch<-sd(x.na) l<-length(x.na) alfa<-(1-p)/2 mu.d<-mu-qt(1-alfa,l-1)*odch/sqrt(l) mu.h<-mu+qt(1-alfa,l-1)*odch/sqrt(l) return(data.frame(mu.d,mu,mu.h)) } result <- mean.int(cbind(rnorm(10),rnorm(10)*2,rnorm(10)+2)) results in data frame result. But I susspect that you use some cycle in your function. In that case you need index variable within for loop. for (i in .... { var[i] <- .... } return(var) HTH Petr On 20 Dec 2005 at 7:39, Andrew Cox wrote: Date sent: Tue, 20 Dec 2005 07:39:27 +0000 (GMT) From: Andrew Cox <apcoxapcox at yahoo.co.uk> To: r-help at stat.math.ethz.ch Subject: [R] Outputing dataframe or vector from within a user defined function> Hi, > I have written a user defined function that carries > out a monte carlo simulation and outputs various > stats. I would like to access and store the simulation > data (a two column local variable) from outside the > function I have written. How can I output the data > from the function as new variable, accessible outside > the function? > > I am probably going to find that this is really > simple, yet I have not been able to find the answer in > (usual places, manuals, 2 x Books and mailing lists) > > Many Thanks > Andy Cox > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz