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
bogdan romocea
2004-Nov-29 21:27 UTC
[R] [BASIC] Solution of creating a sequence of object names
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
Thanks a lot, Adai. I am sure that your tip is very useful for those who are not familiar with 'list'. What is good for using this kind of help list is that you can learn 'additional' info and tips from 'kind' users, which you don't usually expect by reading basic documentations. They are by no means explaining everything in the best understandable way(in fact, this is why we have dozens of textbooks in every subject). I want to add one more small tip for creating object names with numbering. I wanted to generate file names in a following format: 'my01', 'my02', ... , 'my10'. I used the function, 'formatC', with two arguments 'width' and 'flag'. Here is the code,> formatC(99, width=3, flag="0")[1] "099"> > for ( i in 1:10 ) {+ assign(paste("my", formatC(i, width=2, flag="0"), sep=""), NULL) + }> > ls(pat="^my")[1] "my01" "my02" "my03" "my04" "my05" "my06" "my07" "my08" [9] "my09" "my10">But, I do not know if this is covered in basic documentations and if this is a standard way to do the job. I'd be interested in learning more about this. Regards, John --- Adaikalavan Ramasamy <ramasamy at cancer.org.uk> wrote:> Yes, usability is an important aspect too. For > programming efficiency > sake I would suggest a list. Simplified example of > the 3rd solution (by > James Holtman) : > > mylist <-list(NULL) > mylist <-list(NULL) > for(i in 1:3) mylist[[ i ]] <- i > mylist > [[1]] > [1] 1 > > [[2]] > [1] 2 > > [[3]] > [1] 3 > > > Alternative, you can name the indexes > > mylist <-list(NULL) > for(i in 1:3) mylist[[ letters[i] ]] <- i > > mylist > [[1]] > NULL > > $a > [1] 1 > > $b > [1] 2 > > $c > [1] 3 > > > And these can be called by > > mylist$b > [1] 2 > > mylist[["c"]] > [1] 3 > > Keep in mind that you can put and assign different > classes into a list > which makes them very handy. > > a <- list(NULL) > a[[1]] <- "Created on 1st December" > a[[2]] <- c(1,2,3) > a[[3]] <- matrix( 1:4, nc=2 ) > > > > On Mon, 2004-11-29 at 22:53, John wrote: > > It was enough for me to use the 'assign' function > > alone. But I'll remember the 'get' function for > future > > reference. Thanks a lot for the note. > > > > John > > > > > > --- bogdan romocea <br44114 at yahoo.com> wrote: > > > 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 > > > > > > > > > > > > > > > > > > __________________________________ > > > > ______________________________________________ > > 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 > >