[R] Help with "For" instruction

Daniel Nordlund djnordlund at frontier.com
Tue Jan 4 08:28:18 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of ADias
> Sent: Monday, January 03, 2011 10:00 PM
> To: r-help at r-project.org
> 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



More information about the R-help mailing list