我有一个数据集,其中对象在不同时间的值为1或0。我需要一个带有1的函数或一段代码,第一个和最后一个1之间的值为0。

我尝试了complete()和fill(),但没有做我想要的

我有以下数据:

dat = tibble(ID = c(1,1,1,1,1,1,1,1,1,1,
                    2,2,2,2,2,2,2,2,2,2,
                    3,3,3,3,3,3,3,3,3,3),
             TIME = c(1,2,3,4,5,6,7,8,9,10,
                      1,2,3,4,5,6,7,8,9,10,
                      1,2,3,4,5,6,7,8,9,10),
             DV = c(0,0,1,1,0,0,1,0,0,0,
                    0,1,0,0,0,0,0,0,0,1,
                    0,1,0,1,0,1,0,1,0,0))

# A tibble: 30 x 3
      ID  TIME    DV
   <dbl> <dbl> <dbl>
 1     1     1     0
 2     1     2     0
 3     1     3     1
 4     1     4     1
 5     1     5     0
 6     1     6     0
 7     1     7     1
 8     1     8     0
 9     1     9     0
10     1    10     0
# ... with 20 more rows


我需要以下输出,如DV2所示:
dat = tibble(ID = c(1,1,1,1,1,1,1,1,1,1,
                    2,2,2,2,2,2,2,2,2,2,
                    3,3,3,3,3,3,3,3,3,3),
             TIME = c(1,2,3,4,5,6,7,8,9,10,
                      1,2,3,4,5,6,7,8,9,10,
                      1,2,3,4,5,6,7,8,9,10),
             DV = c(0,0,1,1,0,0,1,0,0,0,
                    0,1,0,0,0,0,0,0,0,1,
                    0,1,0,1,0,1,0,1,0,0),
             DV2 = c(0,0,1,1,1,1,1,0,0,0,
                    0,1,1,1,1,1,1,1,1,1,
                    0,1,1,1,1,1,1,1,0,0))

# A tibble: 30 x 4
      ID  TIME    DV   DV2
   <dbl> <dbl> <dbl> <dbl>
 1     1     1     0     0
 2     1     2     0     0
 3     1     3     1     1
 4     1     4     1     1
 5     1     5     0     1
 6     1     6     0     1
 7     1     7     1     1
 8     1     8     0     0
 9     1     9     0     0
10     1    10     0     0
# ... with 20 more rows

最佳答案

使用dplyr,您可以执行以下操作:

dat %>%
 rowid_to_column() %>%
 group_by(ID) %>%
 mutate(DV2 = if_else(rowid %in% min(rowid[DV == 1]):max(rowid[DV == 1]),
                      1, 0)) %>%
 ungroup() %>%
 select(-rowid)

      ID  TIME    DV   DV2
   <dbl> <dbl> <dbl> <dbl>
 1     1     1     0     0
 2     1     2     0     0
 3     1     3     1     1
 4     1     4     1     1
 5     1     5     0     1
 6     1     6     0     1
 7     1     7     1     1
 8     1     8     0     0
 9     1     9     0     0
10     1    10     0     0

10-07 23:57