s-news
[Top] [All Lists]

Re: Removing 0's

To: "Sundar Dorai-Raj" <sundar.dorai-raj@pdf.com>
Subject: Re: Removing 0's
From: jsv@stat.ohio-state.edu
Date: Mon, 21 Nov 2005 08:51:15 -0500 (EST)
Cc: "Bert Jacobs" <b.jacobs@pandora.be>, s-news@lists.biostat.wustl.edu
Importance: Normal
In-reply-to: <4381BAB4.2040809@pdf.com>
References: <20051121115516.DDED9381DD@adicia.telenet-ops.be> <4381BAB4.2040809@pdf.com>
User-agent: SquirrelMail/1.4.5

zeros.trim <- function(x){
non.zeros <- which(!(x==0))
x[non.zeros[1]:non.zeros[length(non.zeros)]]}

_______________________________________________

>
>
> 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
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>



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