Home > database >  Recode id in different time spans
Recode id in different time spans

Time:12-14

I have longitudinal data on the same individuals across different time spans. How do I assign a new id to the same individual when the time is reset.

Here is an example dataset:

df <- data.frame(time=c(1,2,3,4,2,3,4,5,6,3,4),
                 id=c(1,1,1,1,1,1,1,1,1,1,1))
df
   time id
      1  1
      2  1
      3  1
      4  1
      2  1
      3  1
      4  1
      5  1
      6  1
      3  1
      4  1

The expected result is this:

  time id
      1  1
      2  1
      3  1
      4  1
      2  2
      3  2
      4  2
      5  2
      6  2
      3  3
      4  3

CodePudding user response:

Base R for loop

for(i in 2:(nrow(df)-1)){
  df$id[i 1]<-ifelse(df$time[i 1]<df$time[i],df$id[i] 1,df$id[i])
  
}

CodePudding user response:

Assuming that the time column is always in order. With Base R,

cumsum(c(1,diff(df[,"time"])<0))

gives,

[1] 1 1 1 1 2 2 2 2 2 3 3

Of course you can assign it to the id column like,

df[,"id"] <-  cumsum(c(1,diff(df[,"time"])<0))
  • Related