Help with "For" instruction

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with "For" instruction

ADias
Hi,

I am having a problem in doing something similar to this example:

Suppose I have this vector a, and from it I wish to create 5 other vector each one with less one value than what object a has

So I have "a"
a<-c(1,2,3,4,5)

and I want

a1 that shoud have (2,3,4,5)
a2 that should have (1,3,4,5)
a3 that should have (1,2,4,5)
a4 that should have (1,2,3,5)
a5 that should have (1,2,3,4)

I have tried like this but with no luck


For ( i in 1:5) {
a<-c(1,2,3,4,5)
a((i)<-a[-i]
}

Is there a way to do this?

thank you

A.Dias
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

Daniel Nordlund-4
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of ADias
> Sent: Monday, January 03, 2011 10:00 PM
> To: [hidden email]
> Subject: [R] Help with "For" instruction
>
>
> Hi,
>
> I am having a problem in doing something similar to this example:
>
> Suppose I have this vector a, and from it I wish to create 5 other vector
> each one with less one value than what object a has
>
> So I have "a"
> a<-c(1,2,3,4,5)
>
> and I want
>
> a1 that shoud have (2,3,4,5)
> a2 that should have (1,3,4,5)
> a3 that should have (1,2,4,5)
> a4 that should have (1,2,3,5)
> a5 that should have (1,2,3,4)
>
> I have tried like this but with no luck
>
>
> For ( i in 1:5) {
> a<-c(1,2,3,4,5)
> a((i)<-a[-i]
> }
>
> Is there a way to do this?
>
> thank you
>
> A.Dias

Does this do what you want?

for(i in 1:length(a)) assign(paste('a', i, sep=''), a[-i])

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

David Winsemius
In reply to this post by ADias

On Jan 4, 2011, at 1:00 AM, ADias wrote:

>
> Hi,
>
> I am having a problem in doing something similar to this example:
>
> Suppose I have this vector a, and from it I wish to create 5 other  
> vector
> each one with less one value than what object a has
>
> So I have "a"
> a<-c(1,2,3,4,5)
>
> and I want
>
> a1 that shoud have (2,3,4,5)
> a2 that should have (1,3,4,5)
> a3 that should have (1,2,4,5)
> a4 that should have (1,2,3,5)
> a5 that should have (1,2,3,4)
>
> I have tried like this but with no luck
>
> For ( i in 1:5) {
> a<-c(1,2,3,4,5)
> a((i)<-a[-i]
> }
>
> Is there a way to do this?

Dan showed you a method using assign (since that is what is needed for  
what you asked for) but you would get a more flexible result if you  
used a structure that could be easily indexed such as a matrix or list:
 > A <- sapply(1:5, function(i) a[-i])
 > colnames(A) <- paste("a", 1:5, sep="")
 > A
      a1 a2 a3 a4 a5
[1,]  2  1  1  1  1
[2,]  3  3  2  2  2
[3,]  4  4  4  3  3
[4,]  5  5  5  5  4

So:
 > A[ ,"a1"]
[1] 2 3 4 5

>

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

ADias
In reply to this post by Daniel Nordlund-4
Hi

thank you all. I think I have what I need to solve my problem.

Regards,
A.Dias
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

ADias
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
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

ADias
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 <[hidden email]>

> With get().
>
> On Tue, Jan 4, 2011 at 11:58 AM, ADias <[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

Sarah Goslee
2011/1/4 André Dias <[hidden email]>:
> 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 <[hidden email]>
>>
>> With get().
>>
>> On Tue, Jan 4, 2011 at 11:58 AM, ADias <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

Greg Snow-2
In reply to this post by ADias
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of André Dias
> Sent: Tuesday, January 04, 2011 10:55 AM
> To: Sarah Goslee
> Cc: [hidden email]
> 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 <[hidden email]>
>
> > With get().
> >
> > On Tue, Jan 4, 2011 at 11:58 AM, ADias <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

ADias
In reply to this post by Sarah Goslee
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



No dia 4 de Janeiro de 2011 17:58, Sarah Goslee
<[hidden email]>escreveu:

> 2011/1/4 André Dias <[hidden email]>:
> > 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 <[hidden email]>
> >>
> >> With get().
> >>
> >> On Tue, Jan 4, 2011 at 11:58 AM, ADias <[hidden email]> 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with "For" instruction

Sarah Goslee
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 <[hidden email]>:

> 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

______________________________________________
[hidden email] 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.