为每个组生成具有重复和缺失观测值的ID

我有一个数周观察到的个体的数据集。有些人在几周内没有观察到的东西,有些人在同一周内有一些观察到的东西。我需要创建一个特定于个人的每周ID(代码中的id_week)。如果一个人一周内有两个或多个观察值,则两个观察值的id_week应该相同。如果某人在给定的一周内没有观察到数据,则应从最后一个观察点开始进行下一周的观察。这将导致以下数据:

dt<-data.frame(individ=c(1,1,1,2,2,2,3,3,3,3),week=c(1,2,2,1,2,4,1,3,4,4),id_week=c(1,2,2,1,2,3,1,2,3,3))

I have tride dt[, id := .GRP, by = .(individ, week)] but it gives me just ID for weeks, not taken individuals into account. I also tried dplyr solution but it does not account for repeated observations within one week, assigning an ID to every line, which is not what I need.

dt%>%
group_by(individ)%>%
mutate(pp = row_number(week))
评论
  • 俄对花儿笑
    俄对花儿笑 回复

    An option using data.table:

    setDT(dt)[, id_week := rleid(week), individ]