[R] assign

Roger Bivand Roger.Bivand at nhh.no
Wed Jan 7 18:37:49 CET 2004


On Wed, 7 Jan 2004, Perez Martin, Agustin wrote:

> DeaR useRs:
> 
> I would like to assign a values in an object using a loop 'for'.
> This is a reduce example of my problem, my real problem is a few
> complicated:
> 
> for (j in 1:10) {
> 	x.j<-rnorm(100)
> }
> 
> I want to create 10 objects as "x.1, x.2, ... , x.9, x.10" with values in
> it.

Actually, you assigned ten times to object x.j. As was mentioned earlier 
today in reference to a different question, you will almost always find 
that what you think you need and what works best are not the same - a list 
here would be much more convenient:

x <- vector(mode="list", length=10)
for (j in 1:10) {
  x[[j]] <- rnorm(100)
}

for example. For more complicated list elements, the arguments for using 
lists are even stronger. Take a little time to make lists your friends, 
they are powerful and flexible.

> I used the "assign" function but nothing happens.
> Thank you very much
> 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list