1

I want to compare previous row with current row of a column in a dataframe, and subset the higher value alone after a low value. the data is like this,

    > head(counts[,c(2,10:12)],n = 10)
            event_time CLD_Line1 CLD_Line2 CLD_Line3
1  2015-05-01 05:34:00       756       325       735
2  2015-05-01 05:39:00       764       330       742
3  2015-05-01 05:44:00       773       333       752
4  2015-05-01 05:49:00       775       339       764
5  2015-05-01 05:54:00       778       345       768
6  2015-05-01 05:59:00       778       345       768
7  2015-05-01 06:04:00         0       346       770
8  2015-05-01 06:09:00         3       349         4
9  2015-05-01 06:14:00        11         2         6
10 2015-05-01 06:19:00        18         7        12

any function that checks rows with previous rows of a single column i.e,checking row1 is greater than or lesser than to row2 , row2 is greater than or lesser than r3 and so on..?

Magg_rs
  • 323
  • 2
  • 3
  • 12
  • Possible duplicate of [Referring to previous row in calculation](https://stackoverflow.com/questions/34624110/referring-to-previous-row-in-calculation) – Barbara Nov 20 '17 at 12:28
  • Hi, I want to check whether it is greater than or lesser than .. and return true of false value in a new column – Magg_rs Nov 20 '17 at 12:38
  • are you checking all columns? What if there are cases in which one value is greater and one is minor? – Barbara Nov 20 '17 at 12:46

1 Answers1

3

The function diff() applied to a vector gives you the difference between each pair of consecutive values.

Just use it for the column you are interesting in and remember that the result will give you a vector with a length one unit lower than the input values.

R18
  • 1,476
  • 1
  • 8
  • 17
  • I want to check whether it is greater than or lesser than .. and return true of false value in a new column – Magg_rs Nov 20 '17 at 12:32
  • 2
    So, you can use also the sentence `sign(diff(x))` which gives you the sign of the difference. Then you only need to replace the values `-1` and `1` by `TRUE` and `FALSE` as you want. – R18 Nov 20 '17 at 14:24