Skip to content

Help with "For" instruction

5 messages · Sarah Goslee, Greg Snow, André Dias

#
2011/1/4 Andr? Dias <diasandre at gmail.com>:
So how are you using it? It's so much easier to explain what you're doing
wrong if I know what you're doing.

Without a reproducible example I can't show you exactly, but
something like:

 for (i in 1:length(database))
assign(paste("distancematrix",i,sep=""), dist(get(paste("database", i,
sep=""))))

get() is the counterpart of assign(), though there are better (more R-ish)
ways of doing what you want.

Sarah

  
    
#
If you had followed David's advice and put everything into a list or other structure instead of using the assign function (see fortune(236)) then you could just access the list element instead of needing get.  In the long run (or even medium and short run) life will be much easier for you if you learn to use proper data structures and not programmatically create global variables.
#
You need to swap the get and paste commands -
paste() creates the string filename that get() acts on.

As already explained, using a list is much nicer.

Sarah

2011/1/4 Andr? Dias <diasandre at gmail.com>: