Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

identify whenever values repeat in r

I have a dataframe like this.

data <- data.frame(Condition = c(1,1,2,3,1,1,2,2,2,3,1,1,2,3,3))

I want to populate a new variable Sequence which identifies whenever Condition starts again from 1.

So the new dataframe would look like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance for the help!

data <- data.frame(Condition = c(1,1,2,3,1,1,2,2,2,3,1,1,2,3,3),
                   Sequence = c(1,1,1,1,2,2,2,2,2,2,3,3,3,3,3))

>Solution :

base R

data$Sequence2 <- cumsum(c(TRUE, data$Condition[-1] == 1 & data$Condition[-nrow(data)] != 1))
data
#    Condition Sequence Sequence2
# 1          1        1         1
# 2          1        1         1
# 3          2        1         1
# 4          3        1         1
# 5          1        2         2
# 6          1        2         2
# 7          2        2         2
# 8          2        2         2
# 9          2        2         2
# 10         3        2         2
# 11         1        3         3
# 12         1        3         3
# 13         2        3         3
# 14         3        3         3
# 15         3        3         3

dplyr

library(dplyr)
data %>%
  mutate(
    Sequence2 = cumsum(Condition == 1 & lag(Condition != 1, default = TRUE))
  )
#    Condition Sequence Sequence2
# 1          1        1         1
# 2          1        1         1
# 3          2        1         1
# 4          3        1         1
# 5          1        2         2
# 6          1        2         2
# 7          2        2         2
# 8          2        2         2
# 9          2        2         2
# 10         3        2         2
# 11         1        3         3
# 12         1        3         3
# 13         2        3         3
# 14         3        3         3
# 15         3        3         3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading