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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With