[R] For each element in vector do...
ggrothendieck at gmail.com
Tue Jan 17 16:08:10 CET 2006
If addition to the ifelse solution already posted one could do this
since a logical expression used in a numeric context is regarded
as 1 for TRUE and 0 for FALSE.
B <- A + (A>0)
On 1/17/06, Andrej Kastrin <andrej.kastrin at siol.net> wrote:
> Dear R useRs,
> I have a vector with positive and negative numbers:
> 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:
> 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 at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help