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

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Tue 20 Dec 2005 - 19:35:57 EST


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@yahoo.co.uk>
To:             	r-help@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@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

Petr Pikal
petr.pikal@precheza.cz



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:50:02 2005

This archive was generated by hypermail 2.1.8 : Tue 20 Dec 2005 - 20:34:36 EST