> 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 at 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 >
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
B <- ifelse(A > 0, A + 1, A) ?ifelse 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 at 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 >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
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 Address: Kapucijnenvoer 35, Leuven, Belgium 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 > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Andrej Kastrin wrote:> 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... >?ifelse perhaps... > A [1] 0 1 2 3 0 4 5 > B=ifelse(A>0,A+1,0) > B [1] 0 2 3 4 0 5 6 does a sort of element-wise if-else thing. However it evaluates A+1 for all elements. If you have something like: B = ifelse(A>0, slowFunction(A), 0) and not very many zeroes in A, you'll be doing a lot of slowFunction() work for nothing. In which case: B = numeric(length(A)) B[A>0] = slowFunction(A[A>0]) will only pass the necessary values of B to slowFunction() and put them into the right parts of B. B is initially all zeroes. It is left as an exercise to work out the better alternative to: ifelse(A>0, slowFunction(A), otherSlowFunction(A)) Barry
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: > 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: > 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 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
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 at maladmin.com> To: Andrej Kastrin <andrej.kastrin at siol.net> Date sent: Tue, 17 Jan 2006 05:31:53 -0500 Copies to: r-help <r-help at 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 at 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 at 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.htmlPetr Pikal petr.pikal at precheza.cz