用先前的值+1填充零

时间:2019-01-23 16:02:03

标签: r dplyr transform sequence fill

我有一个按用户分组的记录。在变量“ day”处有一些0,我希望按顺序替换它们(=先前值+1)。

data <- data.frame(user = c(1,1,1,2,2,2,2,2), day = c(170,0,172,34,35,0,0,38))

data
  user day
1    1 170
2    1   0
3    1 172
4    2  34
5    2  35
6    2   0
7    2   0
8    2  38

我想要以下内容:


data_new
  user day
1    1 170
2    1 171
3    1 172
4    2  34
5    2  35
6    2  36
7    2  37
8    2  38

我尝试了以下方法(效率很低,并且并非在所有情况下都有效...):

data = group_by(data, user) %>%
+   mutate(lead_day = lead(day),
+          day_new = case_when(day == 0 ~ lead_day - 1,
+                              day > 0 ~ day))
> data
# A tibble: 8 x 4
# Groups:   user [2]
   user   day lead_day day_new
  <dbl> <dbl>    <dbl>   <dbl>
1     1   170        0     170
2     1     0      172     171
3     1   172       NA     172
4     2    34       35      34
5     2    35        0      35
6     2     0        0      -1
7     2     0       38      37
8     2    38       NA      38

1 个答案:

答案 0 :(得分:5)

您可以使用Reduce

data$day <-Reduce(function(x,y) if(y==0) x+1 else y, data$day,accumulate = TRUE)   
data
#   user day
# 1    1 170
# 2    1 171
# 3    1 172
# 4    2  34
# 5    2  35
# 6    2  36
# 7    2  37
# 8    2  38

或者您已经使用tidyverse了:

data %>% mutate(day = accumulate(day,~if(.y==0) .x+1 else .y))
#   user day
# 1    1 170
# 2    1 171
# 3    1 172
# 4    2  34
# 5    2  35
# 6    2  36
# 7    2  37
# 8    2  38