Milktrader
Milktrader

Reputation: 9838

Index out the subsequent row with an identical value in R

I have the following matrix named test:

            High Thresh Sig
2007-02-27 19.01  11.88   1
2007-03-01 19.40  17.29   1
2007-03-02 18.63  17.29   1
2007-03-14 21.25  20.41   1
2007-06-25 17.24  16.70   1
2007-06-27 18.98  18.89   1

I would like to discard the row on 2007-03-02 because it has an identical value in the Thresh column as the preceding day.

I tried this:

test_shorter <- subset(test, diff(Thresh) !=0)

but it indexes out the first occurrence whereas I want to discard the second occurrence.

Upvotes: 3

Views: 118

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

The duplicated function is great for situations like this. For example:

> test[!duplicated(test[,'Thresh']),]
            High Thresh Sig
2007-02-27 19.01  11.88   1
2007-03-01 19.40  17.29   1
2007-03-14 21.25  20.41   1
2007-06-25 17.24  16.70   1
2007-06-27 18.98  18.89   1

If you wanted the same results as what you tried, you can use the fromLast= argument:

> test[!duplicated(test[,'Thresh'], fromLast=TRUE),]
            High Thresh Sig
2007-02-27 19.01  11.88   1
2007-03-02 18.63  17.29   1
2007-03-14 21.25  20.41   1
2007-06-25 17.24  16.70   1
2007-06-27 18.98  18.89   1

Upvotes: 6

Related Questions