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
>
|