[R] how to use different variable to store values with different length in a loop

jim holtman jholtman at gmail.com
Sat Jan 19 04:57:05 CET 2008


I would recommend that you use a "list" instead of creating the
variables.  Here is how you might do a list:

# preallocate the list
myList <- vector('list', 100)
for (i in 1:100){
    .....you calculations.....
    myList[[i]] <- result
}

If you really want to create 100 variables, then use 'assign':

for (i in 1:100){
    .... your calculations....
    assign(paste("A", i, sep=""), result)
}

But a 'list' is much easier to handle especially in that all the
related data is in one object.


On Jan 18, 2008 10:28 PM, Jack Luo <jluo.rhelp at gmail.com> wrote:
> Hi, List
>
> I am trying to use variables named A1, A2, ..., A100 to store some values,
> each variable could store some values with different length, how can I
> achieve this?
>
> Thanks,
>
> Jack
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 you are trying to solve?



More information about the R-help mailing list