[R] Dynamic Variable Names?
Bert Gunter
gunter.berton at gene.com
Sat Apr 14 16:43:44 CEST 2012
Use lists.
e.g.
results <- lapply(1:10,function(i,...){ do stufff } )
will give you a list of results that you can name if you wish to.
You can also explicitly fill the list with the loop if you don't like
lapply -- some folks don't like anonymous functions, for example,
along the lines of:
results <- vector("list:,10)
for(i in 1:10) results[[i]] <- {whatever(i)}
but my aesthetic sense is somehow more offended by this.
Cheers,
Bert
On Sat, Apr 14, 2012 at 6:16 AM, johnny123 <johnny123 at dispostable.com> wrote:
> Hi, I've written some R code that runs through a loop a certain number of
> times. I want to save the output of each loop under a new variable name,
> but I seem unable to do so.
>
> I have a [10,1] matrix of stock tickers, and I want to save the output to
> the variable name: paste(matrix[i,1],"rets",sep="").
> Eg: AAPLrets would be the name of the variable that I would save the output
> to.
> Ideally after the script runs it would have saved 10 output to 10 different
> variable names.
> Any suggestions on how to do so?
>
> Thanks.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Dynamic-Variable-Names-tp4557189p4557189.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list