shalu
2008-Sep-06 21:33 UTC
[R] re ferring to a group of vectors without explicit enumeration
I am trying to define 25 vectors of varying lengths, say y1 to y25 in a loop, and then store the results of some computations in them. My problem is about using some sort of concatenation for names. For example, instead of initializing each of y1 through y25, I would like to do it in a loop. Similar to cat and paste for texts, is there anyway of using y"i" for the vector name where i ranges from 1 to 25, so ultimately it refers to the vector y1,..,y25? Varying lengths is not a problem. To start with each has only length 1 and then I will be adding to each vector based on some results. -- View this message in context: http://www.nabble.com/referring-to-a-group-of-vectors-without-explicit-enumeration-tp19351518p19351518.html Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2008-Sep-06 22:07 UTC
[R] re ferring to a group of vectors without explicit enumeration
shalu <shahlar <at> hotmail.com> writes:> > > I am trying to define 25 vectors of varying lengths, say y1 to y25 in a loop, > and then store the results of some computations in them. My problem is about > using some sort of concatenation for names. For example, instead of > initializing each of y1 through y25, I would like to do it in a loop. > Similar to cat and paste for texts, is there anyway of using y"i" for the > vector name where i ranges from 1 to 25, so ultimately it refers to the > vector y1,..,y25? > Varying lengths is not a problem. To start with each has only length 1 and > then I will be adding to each vector based on some results.I think this is essentially http://cran.r-project.org/doc/FAQ/R-FAQ.html #How-can-I-turn-a-string-into-a-variable? [URL broken in order to make Gmane happy, reassemble it in your browser] the short answer: assign(), but it would work better to use a list instead. Ben Bolker
jim holtman
2008-Sep-06 22:39 UTC
[R] re ferring to a group of vectors without explicit enumeration
I would suggest that you use a list to store the values since it is easier to create and reference:> output <- list() > for (i in 1:10) output[[i]] <- seq(i) > > output[[1]] [1] 1 [[2]] [1] 1 2 [[3]] [1] 1 2 3 [[4]] [1] 1 2 3 4 [[5]] [1] 1 2 3 4 5 [[6]] [1] 1 2 3 4 5 6 [[7]] [1] 1 2 3 4 5 6 7 [[8]] [1] 1 2 3 4 5 6 7 8 [[9]] [1] 1 2 3 4 5 6 7 8 9 [[10]] [1] 1 2 3 4 5 6 7 8 9 10>On Sat, Sep 6, 2008 at 5:33 PM, shalu <shahlar at hotmail.com> wrote:> > I am trying to define 25 vectors of varying lengths, say y1 to y25 in a loop, > and then store the results of some computations in them. My problem is about > using some sort of concatenation for names. For example, instead of > initializing each of y1 through y25, I would like to do it in a loop. > Similar to cat and paste for texts, is there anyway of using y"i" for the > vector name where i ranges from 1 to 25, so ultimately it refers to the > vector y1,..,y25? > Varying lengths is not a problem. To start with each has only length 1 and > then I will be adding to each vector based on some results. > -- > View this message in context: http://www.nabble.com/referring-to-a-group-of-vectors-without-explicit-enumeration-tp19351518p19351518.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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?
David Winsemius
2008-Sep-06 22:46 UTC
[R] re ferring to a group of vectors without explicit enumeration
On Sep 6, 2008, at 6:07 PM, Ben Bolker wrote:> shalu <shahlar <at> hotmail.com> writes: >> >> I am trying to define 25 vectors of varying lengths, say y1 to y25 >> in a loop, >> and then store the results of some computations in them. My problem >> is about >> using some sort of concatenation for names. For example, instead of >> initializing each of y1 through y25, I would like to do it in a loop. >> Similar to cat and paste for texts, is there anyway of using y"i" >> for the >> vector name where i ranges from 1 to 25, so ultimately it refers to >> the >> vector y1,..,y25? >> Varying lengths is not a problem. To start with each has only >> length 1 and >> then I will be adding to each vector based on some results. > > I think this is essentially > > http://cran.r-project.org/doc/FAQ/R-FAQ.html > #How-can-I-turn-a-string-into-a-variable? > > [URL broken in order to make Gmane happy, reassemble it > in your browser] > > the short answer: assign(), but it would work better > to use a list instead.It may help to consider these options: > varnames <- paste("y", 1:25, sep="") > varnames [1] "y1" "y2" "y3" "y4" "y5" "y6" "y7" "y8" "y9" "y10" "y11" "y12" "y13" "y14" "y15" "y16" "y17" "y18" "y19" [20] "y20" "y21" "y22" "y23" "y24" "y25" > varlist <- list(paste("varbl", 1:25, sep="")) > varlist [[1]] [1] "varbl1" "varbl2" "varbl3" "varbl4" "varbl5" "varbl6" "varbl7" "varbl8" "varbl9" "varbl10" "varbl11" [12] "varbl12" "varbl13" "varbl14" "varbl15" "varbl16" "varbl17" "varbl18" "varbl19" "varbl20" "varbl21" "varbl22" [23] "varbl23" "varbl24" "varbl25" -- David Winsemius
shalu
2008-Sep-07 15:46 UTC
[R] re ferring to a group of vectors without explicit enumeration
Thanks very much. This is what I implemented. I found a similar example elsewhere too. It works fine now. jholtman wrote:> > I would suggest that you use a list to store the values since it is > easier to create and reference: > >> output <- list() >> for (i in 1:10) output[[i]] <- seq(i) >> >> output > [[1]] > [1] 1 > > [[2]] > [1] 1 2 > > [[3]] > [1] 1 2 3 > > [[4]] > [1] 1 2 3 4 > > [[5]] > [1] 1 2 3 4 5 > > [[6]] > [1] 1 2 3 4 5 6 > > [[7]] > [1] 1 2 3 4 5 6 7 > > [[8]] > [1] 1 2 3 4 5 6 7 8 > > [[9]] > [1] 1 2 3 4 5 6 7 8 9 > > [[10]] > [1] 1 2 3 4 5 6 7 8 9 10 > >> > > > On Sat, Sep 6, 2008 at 5:33 PM, shalu <shahlar at hotmail.com> wrote: >> >> I am trying to define 25 vectors of varying lengths, say y1 to y25 in a >> loop, >> and then store the results of some computations in them. My problem is >> about >> using some sort of concatenation for names. For example, instead of >> initializing each of y1 through y25, I would like to do it in a loop. >> Similar to cat and paste for texts, is there anyway of using y"i" for the >> vector name where i ranges from 1 to 25, so ultimately it refers to the >> vector y1,..,y25? >> Varying lengths is not a problem. To start with each has only length 1 >> and >> then I will be adding to each vector based on some results. >> -- >> View this message in context: >> http://www.nabble.com/referring-to-a-group-of-vectors-without-explicit-enumeration-tp19351518p19351518.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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? > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/referring-to-a-group-of-vectors-without-explicit-enumeration-tp19351518p19359352.html Sent from the R help mailing list archive at Nabble.com.