Home > Software engineering >  How do I create a column which has the value of the last time a condition was met in a separate row
How do I create a column which has the value of the last time a condition was met in a separate row

Time:10-14

I have data that looks like this:

d <- data.frame(Item = c(1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0)

I would like to create a column where the value is based on the last time a 0 was present in the row d$item. I don't really know how to get started with something like this in R.

Expected outcome is this:

f$recent <- c(NA, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 1, 0, 1, 2, 3, 0)

Where each row is the the most recent observation of 0 (0 = on same row, 1 = previous row, etc.)

edit: Changed row to column, was posting before coffee. Also added expected result.

CodePudding user response:

You can try rle sequence

transform(
  d,
  recent = with(rle(Item), sequence(lengths)) * (Item != 0)
)

which gives

   Item recent
1     1      1
2     0      0
3     0      0
4     1      1
5     1      2
6     1      3
7     1      4
8     1      5
9     1      6
10    0      0
11    0      0
12    1      1
13    0      0
14    1      1
15    1      2
16    1      3
17    0      0

CodePudding user response:

You can do this, with sequence. It calculate the distance to the latest 1.

dif <- diff(c(which(d$Item == 1), length(d$Item)   1))
sequence(dif, 0)
#[1] 0 1 2 0 0 0 0 0 0 1 2 0 1 0 0 0 1

Edit:

dif <- diff(c(1, which(d$Item != 1), length(d$Item)   1))
sequence(dif, 0)
#[1] 0 0 0 1 2 3 4 5 6 0 0 1 0 1 2 3 0
  • Related