[R] how to create automatically names for vectors in a loop?

jim holtman jholtman at gmail.com
Fri May 28 14:38:39 CEST 2010


Before polluting your workspace with objects, look at how you might
use a 'list' to collect them all together, especially if you are going
to do processing on them later as a group, or if you want to easily
save/load them.  You could do the following:

> myList <- list()
> for (i in 1:10) myList[[paste('funny name', i)]] <- runif(10)
>
> myList
$`funny name 1`
 [1] 0.26550866 0.37212390 0.57285336 0.90820779 0.20168193 0.89838968
0.94467527 0.66079779 0.62911404
[10] 0.06178627

$`funny name 2`
 [1] 0.2059746 0.1765568 0.6870228 0.3841037 0.7698414 0.4976992
0.7176185 0.9919061 0.3800352 0.7774452

$`funny name 3`
 [1] 0.93470523 0.21214252 0.65167377 0.12555510 0.26722067 0.38611409
0.01339033 0.38238796 0.86969085
[10] 0.34034900

$`funny name 4`
 [1] 0.4820801 0.5995658 0.4935413 0.1862176 0.8273733 0.6684667
0.7942399 0.1079436 0.7237109 0.4112744
.........

On Thu, May 27, 2010 at 10:43 PM, Zoppoli, Gabriele (NIH/NCI) [G]
<zoppolig at mail.nih.gov> wrote:
> Hi,
>
> I want to generate a number of vectors and store them with different names, like this:
>
> x=1
>
> while (x<100)
>
>  {
>   vector#x# = rnorm(100)
>   x=x+1
> }
>
> where each vector has, at its hand, instead of #x# a number which goes from 1 to 99.
>
> How can I do this?
>
> Thanks
>
> Gabriele Zoppoli, MD
> Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy
> Guest Researcher, LMP, NCI, NIH, Bethesda MD
>
> Work: 301-451-8575
> Mobile: 301-204-5642
> Email: zoppolig at mail.nih.gov
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list