[R] [BASIC] Solution of creating a sequence of object names

bogdan romocea br44114 at yahoo.com
Mon Nov 29 22:27:36 CET 2004


You may be missing something. After you create all those objects,
you'll want to use them. Use get():
for (i in 1:10) ... get(paste("object",i,sep="")) ... 
It took me about a week to find out how to do this. I waited for a
few days, but before I got to ask this basic/rtfm question, someone
else - fortunately :-) - did.

HTH,
b.


-----Original Message-----
From: John [mailto:cyracules at yahoo.co.uk]
Sent: Monday, November 29, 2004 4:03 PM
To: r-help at stat.math.ethz.ch
Subject: [R] [BASIC] Solution of creating a sequence of object names


Dear R-users,

I state that this is for beginners, so you may ignore
this in order not to be irritated.

By the way, "patience" is another important thing,
together with "kindness", we should keep in mind when
we teach students and our own children as Jim Lemon
pointed out well in the context of the Socratic
method. You may know that being kind does not mean 
giving "spoonfed" answers to questioners.

---------------------

I was asked for the solution of my problem, and a
couple of answers were given to me in private emails.
I am not sure if it was a mere accident. I post them
now, without their permission, for those who are
interested in learning them. So if you're happy to
know the solution, thanks should go to the person
concerned. I thank all the three people named below.

(1) my solution after reading the R-FAQ 7.21 as Uwe
Ligges pointed out

> for ( i in 1:10 ) {
+ assign(paste("my.file.", i, sep=""), NULL)
+ }
>

(2) Adai Ramasamy's solution

> for(obj in paste("my.ftn", 1:10, sep=""))
assign(obj, NULL)
> 
### or 
> 
> for(i in 1:10) assign(paste("my.ftn", i, sep=""),
NULL)
>

(3) James Holtman's solution

# For example, if you want to generate 10 groups 
# of 5 random numbers and store them 
# under then names "GRPn" where n is 1 -> 10, 
# the following can be used:
#
> Result <- list()      # create the list
> for (i in 1:10) Result[[paste("GRP", i, sep='')]] <-
runif(5)   # store each result
> Result    # print out the data
$GRP1
[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819

$GRP2
[1] 0.89838968 0.94467527 0.66079779 0.62911404
0.06178627

$GRP3
[1] 0.2059746 0.1765568 0.6870228 0.3841037 0.7698414

$GRP4
[1] 0.4976992 0.7176185 0.9919061 0.3800352 0.7774452

$GRP5
[1] 0.9347052 0.2121425 0.6516738 0.1255551 0.2672207

$GRP6
[1] 0.38611409 0.01339033 0.38238796 0.86969085
0.34034900

$GRP7
[1] 0.4820801 0.5995658 0.4935413 0.1862176 0.8273733

$GRP8
[1] 0.6684667 0.7942399 0.1079436 0.7237109 0.4112744

$GRP9
[1] 0.8209463 0.6470602 0.7829328 0.5530363 0.5297196

$GRP10
[1] 0.78935623 0.02333120 0.47723007 0.73231374
0.69273156

>

Regards,

John

______________________________________________
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




More information about the R-help mailing list