Hi Edward, In your post, rather than asking for the indexes, you asked for the numbers, AFAICS. Now, whether you want the indexes in which (b-a) is either positive or negative, then do
which(b-a>0) # Positives # [1] 2 3 6 7 which(b-a<0) # Negatives # [1] 1 4 5 (a-b)[b<a] # Henrique's solution to what you asked first # [1] 1 3 1 Am I missing something here? Could be :-) HTH, Jorge On Mon, Sep 14, 2009 at 5:14 PM, <edche...@gmail.com> wrote: > thank you all for your help. I do know how to use which() but my problem > is that I am writing a function in which this is just part of it. After > seeing the (a-b)[b<a], it gives the wrong index number for which is negative > and which is positive. I am not sure why that is, but the which function > does give the correct index number. I guess what I want is to be able to > save two vectors of index and use them to reference the raw data base for > further calculation. One vector for all the negative values and one for all > the positive ones. > > > Edward Chen > Email: tke...@msn.com > Cell Phone: 510-371-4717 > > > > > ------------------------------ > From: www...@gmail.com > Date: Mon, 14 Sep 2009 16:35:23 -0300 > Subject: Re: [R] Help with for loop > To: dwinsem...@comcast.net > CC: jorgeivanve...@gmail.com; r-help@r-project.org; edche...@gmail.com > > > Or: > > (a - b)[b < a] > > On Mon, Sep 14, 2009 at 4:16 PM, David Winsemius > <dwinsem...@comcast.net>wrote: > > > On Sep 14, 2009, at 3:02 PM, Jorge Ivan Velez wrote: > > Hi Edward, > Here is a suggestion: > > a = c(4,5,1,7,8,12,39) > b = c(3,7,8,4,7,25,78) > d <- a-b > d[which(d>0)] > # [1] 1 3 1 > > > #Or even: > d <- (a-b)[which((a-b)>0)] > d > #[1] 1 3 1 > > > > HTH, > Jorge > > > On Mon, Sep 14, 2009 at 2:50 PM, Edward Chen <edche...@gmail.com> wrote: > > I have a code: > *a = c(4,5,1,7,8,12,39) > b = c(3,7,8,4,7,25,78) > d =a-b > for(i in 1:length(d)){ > if(d[i]>0){x = list(d[i]) > print(x)} > else{y = list(d[i]) > print(y)}} > > the results are: > > [[1]] > [1] 1 > > [[1]] > [1] -2 > > [[1]] > [1] -7 > > [[1]] > [1] 3 > > [[1]] > [1] 1 > > [[1]] > [1] -13 > > [[1]] > [1] -39 > > > which will tell me what d is. but is it possible to output the order in > which the difference is in the vector d? > for example I would want to see x = 1,3,1 and they are from d[1], d[4], > d[5]. > This is just a crude example I thought of to help me do something more > complicated. > > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.