s-news
[Top] [All Lists]

Re: Removing 0's

To: Bert Jacobs <b.jacobs@pandora.be>
Subject: Re: Removing 0's
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Mon, 21 Nov 2005 06:16:52 -0600
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <20051121115516.DDED9381DD@adicia.telenet-ops.be>
Organization: PDF Solutions, Inc.
References: <20051121115516.DDED9381DD@adicia.telenet-ops.be>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)


Bert Jacobs wrote:
Hi,

Using S-Plus 6.2 for Windows XP

I have a vector of values that looks like this:

0
0
12
15
16
0
0
18
4
3
2
0
0

Does there exist an elegant way to achieve the following =removing the 0's at the start of the vector (if there are any) and at the
end of the vector (if there are any):

12
15
16
0
0
18
4
3
2

Thx for helping me out.
Bert



How about:

wz <- which(diff(x == 0) != 0)
x[(wz[1] + 1):wz[length(wz)]]

This assumes you always have zeros present. It would have to be re-worked in the cases were you do no have zeros either at the beginning, the end of the vector, or both.

--sundar

<Prev in Thread] Current Thread [Next in Thread>