Re: [R] Outputing dataframe or vector from within a user defined function

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Tue 20 Dec 2005 - 19:31:35 EST

Andrew Cox wrote:

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

It's certainly in all books about R that I know as well as in the manuals. And you are using it all the time for other functions: assignments.

Simply speaking, the last value of a function is returned. You can also explicitly call return(). You have to assign the value to a new variable when you call the function. Example:

foo <- function(){

   x <- rnorm(10)
   y <- x*5
   return(list(x=x, y=y))
}

result <- foo()
result

Uwe Ligges

> Many Thanks
> Andy Cox
>
> ______________________________________________
> R-help@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



R-help@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 Received on Tue Dec 20 19:41:57 2005

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 05 May 2006 - 12:10:02 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.