Alternatively if you want to avoid using lists, you can create the variables
like this:
for (i in 1:10) assign(paste("x",i,sep=""),i^2)
and view them subsequently like this:
for (i in 1:10) print(get(paste("x",i,sep="")))
Phil
-----Original Message-----
From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de]
Sent: 05 December 2002 12:58
To: Jan Wiener
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] (no subject)
Jan Wiener wrote:>
> hi,
> suppose you have a for-loop like this:
>
> for(i in1:x) {} ...
>
> now you want to generate a variable in every
> cycle (since you do not know the size of x in
> beforehand you have to do this dynamically).
>
> the variable should e.g. look like this:
> variableName1<-c() (if x==1)
> variableName2<-c() (ifx==2)
> variableName3<-c() (if x==3)
> ..
> ..
> i tried this(which obviously didn't work):
>
> paste("variableName",x,sep="")<-c(2,3,4)
>
> so i need
> something similiar to this (but working!)!
- This is FAQ 7.23 (see the FAQ's, please).
- I'd suggest to use a list instead of a couple of different objects.
- See ?assign for more details related to your question:
assign(paste("variableName", x, sep=""), c(2,3,4))
Uwe Ligges
______________________________________________
R-help at stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com