I am trying to define a large number of variables through a loop construct. I have my loop variable i being cycled through 1:100 and I would like the variables produced by this to be called vi (i.e. v1 v2 v3 etc) so, for example I'm going: for(i in 1:100) { <blank> <- a[i:N] # or whatever else you want to put on the right side } where N is previously defined. What goes in for <blank>? Thanks, Toby
On Tue, 30 Nov 2004, Tobias Muhlhofer wrote:> I am trying to define a large number of variables through a loop construct. > > I have my loop variable i being cycled through 1:100 and I would like the > variables produced by this to be called > > vi (i.e. v1 v2 v3 etc) >Look at FAQ 7.21, which explains how to do it and suggest that you might not really want to. -thomas
See the R-FAQ here: http://cran.r-project.org/doc/FAQ/R-FAQ.html Number 7.21. However, as the FAQ also points out, you might be better served using lists. a <- list() for (i in 1:100) { a[[i]] <- some stuff } Sean On Nov 30, 2004, at 5:42 PM, Tobias Muhlhofer wrote:> I am trying to define a large number of variables through a loop > construct. > > I have my loop variable i being cycled through 1:100 and I would like > the variables produced by this to be called > > vi (i.e. v1 v2 v3 etc) > > so, for example I'm going: > > for(i in 1:100) { > > <blank> <- a[i:N] # or whatever else you want to put on the right side > > } > > where N is previously defined. > > What goes in for <blank>? > > Thanks, > Toby > > ______________________________________________ > 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
see ?assign and ?get i,e instead ob <blank> below have something like assign(paste("v", i, sep=""), a[i:N]) and if you need to loop over calling them say get(paste("v", i, sep="")) of course typing v1 will call the variable... Jean On Tue, 30 Nov 2004, Tobias Muhlhofer wrote:> I am trying to define a large number of variables through a loop construct. > > I have my loop variable i being cycled through 1:100 and I would like > the variables produced by this to be called > > vi (i.e. v1 v2 v3 etc) > > so, for example I'm going: > > for(i in 1:100) { > > <blank> <- a[i:N] # or whatever else you want to put on the right side > > > } > > where N is previously defined. > > What goes in for <blank>? > > Thanks, > Toby > > ______________________________________________ > 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 >
Tobias Muhlhofer <t.muhlhofer at lse.ac.uk> writes:> I am trying to define a large number of variables through a loop construct. > > I have my loop variable i being cycled through 1:100 and I would like > the variables produced by this to be called > > vi (i.e. v1 v2 v3 etc) > > so, for example I'm going: > > for(i in 1:100) { > > <blank> <- a[i:N] # or whatever else you want to put on the right side > } > > where N is previously defined. > > What goes in for <blank>?...> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html... Did you? Not to put too fine a point on it, but this particular question spurred a lengthy thread over the weekend about how polite one should be (or not) to people who obviously haven't read the FAQ... This is Question 7.21, to be precise. Short answer: use assign(paste(...)) if you must, but you're usually better off constructing a list, for instance like this:> a <- 1:5 ; l <- lapply(1:5,function(i) a[i:5]) > l[[1]] [1] 1 2 3 4 5 [[2]] [1] 2 3 4 5 [[3]] [1] 3 4 5 [[4]] [1] 4 5 [[5]] [1] 5 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Tobias Muhlhofer <t.muhlhofer at lse.ac.uk> wrote: I am trying to define a large number of variables through a loop construct. He wants to do for (i in 1:100) { assign(paste("v", i, sep=""), ....something or other...) } This is, of course, a FAQ. It's such a FAQ that I must have seen it once a day for the last several days. What I want to know is *WHY* people are doing this? What, precisely, does it buy you to have variables called v1...v100 rather than variables called v[[1]]...v[[100]]? Apart from persistent inconvenience, that is? What, really, is wrong with v <- lapply(1:100, function (i) ....something or other...)
I wrote about the perennial "assign to V1 ... Vn" problem: > >What I want to know is *WHY* people are doing this? I failed to make myself clear. What I meant was "what happens NEXT? Once someone has got past the stage of generating V1 ... V100 (or whatever the magic number is), what do they intend to DO with them? I think this is the important question, and I think that this is what the FAQ needs to give help with, and once I understand what people are trying to accomplish *overall* I will be happy to offer some text for the FAQ. I've used stats packages myself that let you abbreviate a range of variables. The thing is that this was supported by *analysis* methods (and output methods), not just *input* methods. Now it seems to me that pretty much everything I would want to do with a bunch of separate variables like this in such a package would mean in R that I desperately wanted these things to be columns in a data frame. Perhaps the next time someone asks this question we can ask them what they intend to do with the variables once they have them. On past history, we shan't have to wait very long.