Re: [R] For each element in vector do...

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Wed 18 Jan 2006 - 19:01:29 EST

Hi

Or mabe easier in this case

b <- a+1-(a==0)

HTH
Petr

On 17 Jan 2006 at 5:31, tom wright wrote:

From:           	tom wright <tom@maladmin.com>
To:             	Andrej Kastrin <andrej.kastrin@siol.net>
Date sent:      	Tue, 17 Jan 2006 05:31:53 -0500
Copies to:      	r-help <r-help@stat.math.ethz.ch>
Subject:        	Re: [R] For each element in vector do...


> > a<-c(0,1,2,3,0,4,5)
> > b<-vector(length=length(a))
> > b[a>0]<-a[a>0]+1
> > b[a<=0]<-a[a<=0]
> > b
> [1] 0 2 3 4 0 5 6
>
>
> On Tue, 2006-17-01 at 15:30 +0100, Andrej Kastrin wrote:
> > Dear R useRs,
> >
> > I have a vector with positive and negative numbers:
> > A=c(0,1,2,3,0,4,5)
> >
> > Now if i-th element in vector A is > 0, then i-th element in vector
> > B is a+1 else i-th element in vector b=a (or 0)
> >
> > vector A: 0 1 2 3 0 4 5
> > vector B: 0 2 3 4 0 5 6
> >
> > What's the right way to do this. I still have some problems with for
> > and if statements...
> >
> > Cheers, Andrej
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal@precheza.cz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 18 19:13:05 2006

This archive was generated by hypermail 2.1.8 : Wed 18 Jan 2006 - 22:08:05 EST