# [R] For each element in vector do...

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Jan 17 15:42:24 CET 2006

```one way is:

a <- c(0,1,2,3,0,4,5)
b <- ifelse(a > 0, a + 1, a)
b

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Andrej Kastrin" <andrej.kastrin at siol.net>
To: "r-help" <r-help at stat.math.ethz.ch>
Sent: Tuesday, January 17, 2006 3:30 PM
Subject: [R] For each element in vector do...

> 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 at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help