Dear All, For any given vector, I want to extract a sub-vector such that the new vector skips all zeros, if any , at the start of vector. Is it possible to achieve this w/o looping? E.G : > x = c(0,0,1,2,3,4,5,0,0,8,9) > y = somefunc(x); > y [1] 1 2 3 4 5 0 0 8 9 In the example above, I want to skip the two leading zeroes till I hit the non-zero (1 in the above case). I also want to retain all zero after the first non-zero digit (again 1 in this case). Hope I am not confusing you guys. Thanks in advance for your help. Regards Manoj [[alternative HTML version deleted]]
I should clarify that I tried x[cumsum(x)!=0] but the problem is that I might have negative numbers in the vector that can potentially make cumsum(x) equal to zero somewhere down the line in the vector. Manoj ----- Original Message ----- From: ManojW To: R-help Sent: Monday, May 30, 2005 2:00 PM Subject: Vector Manipulation Dear All, For any given vector, I want to extract a sub-vector such that the new vector skips all zeros, if any , at the start of vector. Is it possible to achieve this w/o looping? E.G : > x = c(0,0,1,2,3,4,5,0,0,8,9) > y = somefunc(x); > y [1] 1 2 3 4 5 0 0 8 9 In the example above, I want to skip the two leading zeroes till I hit the non-zero (1 in the above case). I also want to retain all zero after the first non-zero digit (again 1 in this case). Hope I am not confusing you guys. Thanks in advance for your help. Regards Manoj [[alternative HTML version deleted]]
OK...x[min(which(x!=0)):length(x)] does the trick! I guess the coffee is slowly but surely working! . Manoj ----- Original Message ----- From: ManojW To: R-help Sent: Monday, May 30, 2005 2:12 PM Subject: Re: Vector Manipulation I should clarify that I tried x[cumsum(x)!=0] but the problem is that I might have negative numbers in the vector that can potentially make cumsum(x) equal to zero somewhere down the line in the vector. Manoj ----- Original Message ----- From: ManojW To: R-help Sent: Monday, May 30, 2005 2:00 PM Subject: Vector Manipulation Dear All, For any given vector, I want to extract a sub-vector such that the new vector skips all zeros, if any , at the start of vector. Is it possible to achieve this w/o looping? E.G : > x = c(0,0,1,2,3,4,5,0,0,8,9) > y = somefunc(x); > y [1] 1 2 3 4 5 0 0 8 9 In the example above, I want to skip the two leading zeroes till I hit the non-zero (1 in the above case). I also want to retain all zero after the first non-zero digit (again 1 in this case). Hope I am not confusing you guys. Thanks in advance for your help. Regards Manoj [[alternative HTML version deleted]]
x[cumsum(x!=0)!=0] or x[!!cumsum(!!x)] will also do it. On 5/30/05, ManojW <manojsw at gmail.com> wrote:> OK...x[min(which(x!=0)):length(x)] does the trick! > > I guess the coffee is slowly but surely working! . > > Manoj > ----- Original Message ----- > From: ManojW > To: R-help > Sent: Monday, May 30, 2005 2:12 PM > Subject: Re: Vector Manipulation > > > I should clarify that I tried x[cumsum(x)!=0] but the problem is that I might have negative numbers in the vector that can potentially make cumsum(x) equal to zero somewhere down the line in the vector. > > Manoj > ----- Original Message ----- > From: ManojW > To: R-help > Sent: Monday, May 30, 2005 2:00 PM > Subject: Vector Manipulation > > > Dear All, > For any given vector, I want to extract a sub-vector such that the new vector skips all zeros, if any , at the start of vector. Is it possible to achieve this w/o looping? > > E.G : > x = c(0,0,1,2,3,4,5,0,0,8,9) > > y = somefunc(x); > > y > [1] 1 2 3 4 5 0 0 8 9 > > In the example above, I want to skip the two leading zeroes till I hit the non-zero (1 in the above case). I also want to retain all zero after the first non-zero digit (again 1 in this case). > > Hope I am not confusing you guys. Thanks in advance for your help. > > Regards > > Manoj > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >