Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.61.0505182322200.22918@gannet.stats>
Date: 2005-05-18T22:26:26Z
From: Brian Ripley
Subject: loop
In-Reply-To: <3f87cc6d05051813585fb249bc@mail.gmail.com>

On Wed, 18 May 2005, Omar Lakkis wrote:

> Rather than using a loop, how can I remove all consequentially
> repeated values as in this example?
> I am guessing using diff would help but not quite sure how.
>
>> get
>> s
>        date          f
> 1 1999-01-01 1
> 2 1999-01-02 1
> 3 1999-01-03 1
> 4 1999-01-04 2
> 5 1999-01-05 2
>
>> v <- s[1,'f']; for (i in 2:nrow(s)) { if (s[i,'f'] == v) s[i,'f'] <- NA else v <- s[i,'f'] }
>> s <- s[!is.na(s$f),]
>> s
>        date          f
> 1 1999-01-01 1
> 4 1999-01-04 2

I am not sure I see the pattern, but it might be one of

s[!duplicated(s$f), ]

s[diff(c(0, s$f)) != 0, ]

(which differ).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595