Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying when a sequence stops

Tags:

r

sequence

new here so apologies if this question has been answered before. I am trying to identify a sequence within a dataset.

Example:

id lengthofstay
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
3 4

Then I want to create a variable that will be 0 when the sequence is still going and 1 when the sequence ends.

E.g.

id lengthofstay newvariable
1 1 0
1 2 0
1 3 1
2 1 0
2 2 1
3 1 0
3 2 0
3 3 0
3 4 1

Thank you in advance for your help. Please let me know if there is anything else I can provide to be of use.

I am quite new to R so I apologize but I am at a loss to where to start. Thank you again for your help!

like image 844
cyoonit Avatar asked Sep 11 '25 03:09

cyoonit


1 Answers

You can use ave + max

> transform(df, newvariable = +(ave(lengthofstay, id, FUN = max) == lengthofstay))
  id lengthofstay newvariable
1  1            1           0
2  1            2           0
3  1            3           1
4  2            1           0
5  2            2           1
6  3            1           0
7  3            2           0
8  3            3           0
9  3            4           1
like image 174
ThomasIsCoding Avatar answered Sep 13 '25 16:09

ThomasIsCoding