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

Petr Pikal petr.pikal at precheza.cz
Tue Dec 20 09:35:57 CET 2005


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.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list