lundi 30 mars 2020

Create new column when have repeat values and when have break points

Im trying to create a new column with some conditions that I dont know yet how to do, I have this data frame for example

area
AR
AR
AR
AM
AM
AR
AR
AR
AM
AM
AM
AM
...
AM

So, when have AR x times and after this have one or until 20 AM and came back have AR, I want a new column with AR. By the moment when have AM x times and only AM, without come back to AR, I want new column with AM. Like this:

area    area2
AR      AR
AR      AR
AR      AR
AM      AR
AM      AR
AR      AR
AR      AR
AR      AR
AM      AM
AM      AM
AM      AM
AM      AM
...     ...
AM      AM

I tried to do with sequence, but I don't know if is the best way

df$seq <- sequence(rle(as.character(df$area))$lengths)

Someone know how to do? Thanks!

Aucun commentaire:

Enregistrer un commentaire