An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110104/52907e86/attachment.pl>
Help with "For" instruction
5 messages · Sarah Goslee, Greg Snow, André Dias
2011/1/4 Andr? Dias <diasandre at gmail.com>:
hi how do I exactly use the get(). I am reading the help for get() but the way I am using it causes an error/
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
thanks ADias 2011/1/4 Sarah Goslee <sarah.goslee at gmail.com>
With get(). On Tue, Jan 4, 2011 at 11:58 AM, ADias <diasandre at gmail.com> wrote:
Hi,
Still with the above problem:
But for instance, i have a data base with 30 variables and I created an
object each with one varibale missing:
DataBase - has 30 variables
DataBase1 has 29 variables with the 1st variable gone
DataBase2 has 29 variables with the 2nd variable gone
for(i in 1:length(database))
assign(paste("database",i,sep=""),database[-i])
Now, I wish to create the 30 distance matrix:
for (i in 1:length(database))
assign(paste("distancematrix",i,sep=""),
dist(database[i]))
But doing like this - database[i] - I am just refering to the 1st value
on
the object database and not to the entire database i.
How do I do this?
thanks
Regards,
A.Dias
--
--
Sarah Goslee http://www.functionaldiversity.org
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.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Andr? Dias
> Sent: Tuesday, January 04, 2011 10:55 AM
> To: Sarah Goslee
> Cc: r-help at r-project.org
> Subject: Re: [R] Help with "For" instruction
>
> hi
>
> how do I exactly use the get(). I am reading the help for get() but the
> way
> I am using it causes an error/
>
> thanks
> ADias
>
> 2011/1/4 Sarah Goslee <sarah.goslee at gmail.com>
>
> > With get().
> >
> > On Tue, Jan 4, 2011 at 11:58 AM, ADias <diasandre at gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > Still with the above problem:
> > >
> > > But for instance, i have a data base with 30 variables and I
> created an
> > > object each with one varibale missing:
> > >
> > > DataBase - has 30 variables
> > > DataBase1 has 29 variables with the 1st variable gone
> > > DataBase2 has 29 variables with the 2nd variable gone
> > >
> > > for(i in 1:length(database))
> > assign(paste("database",i,sep=""),database[-i])
> > >
> > >
> > > Now, I wish to create the 30 distance matrix:
> > >
> > > for (i in 1:length(database))
> > > assign(paste("distancematrix",i,sep=""),
> > > dist(database[i]))
> > >
> > > But doing like this - database[i] - I am just refering to the 1st
> value
> > on
> > > the object database and not to the entire database i.
> > >
> > > How do I do this?
> > >
> > > thanks
> > > Regards,
> > > A.Dias
> > > --
> > --
> > Sarah Goslee
> > http://www.functionaldiversity.org
> >
>
> [[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110104/2966d81f/attachment.pl>
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>:
Hi
I was doing
for (i in 1:length(database))
assign(paste("distancematrix",i,sep=""), dist(paste(get("database", i,
sep="")))))
but i really did not know what I was doing. I will try?your way. But I still
don't understnad how the get function works.
What woud be more r-ish then get() ?
thanks
ADias
Sarah Goslee http://www.functionaldiversity.org