[R] loop

From: Omar Lakkis <uofiowa_at_gmail.com>
Date: Thu 19 May 2005 - 06:58:13 EST


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



R-help@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 Received on Thu May 19 08:03:00 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:50 EST