Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove identical values if the same as previous in a time series

I have a time series:

 df <- data.frame(t=1:10, x= c(5,7,8,9,5,5,5,5,4,3))

I want to remove values that are identical to the previous value to obtain:

 x = c(5,7,8,9,5,4,3)

I tried:

 df[unique(df$x),]

But this gives the incorrect answer.

like image 328
adam.888 Avatar asked Jan 26 '26 04:01

adam.888


2 Answers

You can do:

df[c(1, diff(df$x)) != 0, ] 

   t x
1  1 5
2  2 7
3  3 8
4  4 9
5  5 5
6  9 4
7 10 3
like image 133
Ritchie Sacramento Avatar answered Jan 27 '26 19:01

Ritchie Sacramento


With dplyr, you can do:

df %>%
 filter(x != lag(x, default = first(x)-1))

   t x
1  1 5
2  2 7
3  3 8
4  4 9
5  5 5
6  9 4
7 10 3
like image 40
tmfmnk Avatar answered Jan 27 '26 18:01

tmfmnk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!