Skip to content

how to create automatically names for vectors in a loop?

4 messages · Zoppoli, Gabriele (NIH/NCI) [G], Erik Iverson, Lukas Schefczyk +1 more

#
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
#
Zoppoli, Gabriele (NIH/NCI) [G] wrote:
?paste and ?assign but almost surely you'd be better off storing these vectors 
in a list or matrix.
#
Hallo,

x=1

while (x<100)

 {
   vectorx<- rnorm(100)
    assign(paste("vector",x,sep=""),vectorx)
x=x+1

}
ls()

HTH
Lukas Schefczyk

--------------------------------------------------
From: "Zoppoli, Gabriele (NIH/NCI) [G]" <zoppolig at mail.nih.gov>
Sent: Friday, May 28, 2010 4:43 AM
To: <r-help at r-project.org>
Subject: [R] how to create automatically names for vectors in a loop?
#
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:
$`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: