Outputing dataframe or vector from within a user defined function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Outputing dataframe or vector from within a user defined function

Andrew Cox
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

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

PIKAL Petr
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 <[hidden email]>
To:             [hidden email]
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html