[R] function input as variable name (deparse/quote/paste) ??

Berend Hasselman bhh at xs4all.nl
Sun Mar 11 06:49:03 CET 2012


On 11-03-2012, at 01:01, casperyc wrote:

> Sorry if I wasn't stating what I really wanted or it was a bit confusing.
> 
> Basically, there are MANY datasets to run suing the same function
> 
> I have written a function to analyze it and returns a LIST of useful out put
> in the variable 'res' (to the workspace).
> 

Your function uses return?
Probably not.

> I also created another script run.r such as
> 
> myname(dat1)
> myname(dat2)
> myname(dat3)
> myname(dat4)
> myname(dat5) 
> 
> For now, each time the output in the main workspace 'res' (the list) is over
> written.
> 
> I want it to have different suffix to differentiate them. So I can have a
> look later after the batch is run.

Well, if that is the case then there is a better way than doing global assignments in a function.

Make sure myfunction returns the list of results with return() and don't do global assignment with <<-

for( k in 1:5) {
	dataname <- paste("data",k,sep="")
        resname   <- paste("res",k,sep="")
        assign(resname, myfunction(get(dataname)))
}


Berend



More information about the R-help mailing list