[R] re ferring to a group of vectors without explicit enumeration
David Winsemius
dwinsemius at comcast.net
Sun Sep 7 00:46:24 CEST 2008
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
More information about the R-help
mailing list