Is it possible to give a temporary object a name that varies with each run of a
foreloop? For example, I want to fill a matrix every time I run a loop, and I
want a new matrix with each run, with an appropriate new name.
i.e.:
for(i in 1:5){... matrix.i<-some values ...}
so that in the end I would have:
matrix.1
matrix.2
matrix.3
matrix.4
matrix.5
Thanks,
Ben Osborne
--
Botany Department
University of Vermont
109 Carrigan Drive
Burlington, VT 05405
benjamin.osborne at uvm.edu
phone: 802-656-0297
fax: 802-656-0440
variable object naming
4 messages · Benjamin M. Osborne, Gabor Grothendieck, Thomas Lumley +1 more
Benjamin M. Osborne <Benjamin.Osborne <at> uvm.edu> writes:
:
: Is it possible to give a temporary object a name that varies with each run
of a
: foreloop? For example, I want to fill a matrix every time I run a loop, and
I
: want a new matrix with each run, with an appropriate new name.
: i.e.:
: for(i in 1:5){... matrix.i<-some values ...}
:
: so that in the end I would have:
: matrix.1
: matrix.2
: matrix.3
: matrix.4
: matrix.5
See 7.1 of the FAQ.
On Mon, 22 Nov 2004, Gabor Grothendieck wrote:
Benjamin M. Osborne <Benjamin.Osborne <at> uvm.edu> writes:
:
: Is it possible to give a temporary object a name that varies with each run
of a
: foreloop? For example, I want to fill a matrix every time I run a loop, and
I
: want a new matrix with each run, with an appropriate new name.
: i.e.:
: for(i in 1:5){... matrix.i<-some values ...}
:
: so that in the end I would have:
: matrix.1
: matrix.2
: matrix.3
: matrix.4
: matrix.5
See 7.1 of the FAQ.
Especially the part that says this probably isn't what you want to do. You
would likely be better off with a list, and doing
matrixlist[[i]]<-some_values
-thomas
Is this, i.e., using 'assign', the direction you want to go? If so, pay attention to the 'envir' parameter. BEGIN FROM Stat-R-US: http://www.ku.edu/~pauljohn/R/Rtips.html#2.2 ... 2.2 Create variable names "on the fly" (10/04/2001 Paul Johnson <pauljohn at ukans.edu>) Paste together a variable name, set it to a value. Use assign. As in
assign(paste("file", 1, "max", sep=""), 1)
ls()
[1] "file1max" (Brian Ripley, June 18, 2001) ... END FROM STAT-R-US Regards, Michael Grant --- "Benjamin M. Osborne" <Benjamin.Osborne at uvm.edu> wrote:
Is it possible to give a temporary object a name
that varies with each run of a
foreloop? For example, I want to fill a matrix
every time I run a loop, and I
want a new matrix with each run, with an appropriate
new name.
i.e.:
for(i in 1:5){... matrix.i<-some values ...}
so that in the end I would have:
matrix.1
matrix.2
matrix.3
matrix.4
matrix.5
Thanks,
Ben Osborne
--
Botany Department
University of Vermont
109 Carrigan Drive
Burlington, VT 05405
benjamin.osborne at uvm.edu
phone: 802-656-0297
fax: 802-656-0440
______________________________________________ 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