重复行并改变条件语句的多个观察值

Repeat row and mutate multiple observations on conditional statement

我正在尝试编写一个条件语句来复制 df$food=1 的行。然后将初始行的值“df$weight”更改为 df$prior_w 的值而不是重复项。我还需要将重复项上的 df$food 值更改为 0,并在重复项上将 prior_w 更改为 NA

df <- data.frame(date=("2022-01-01","2022-01-02","2022-01-03","2022-01-04","2022-01-05"),
food=(0,0,0,1,0),
prior_w=(NA,NA,NA,2,NA),
weight=(5,4,3,6,4))

我喜欢这样的数据框

df_2 <- data.frame(date=("2022-01-01","2022-01-02","2022-01-03","2022-01-04","2022-01-04","2022-01-05"),
food=(0,0,0,1,0,0),
prior_w=(NA,NA,NA,NA,2,NA),
weight=(5,4,3,2,6,4))

我会用文字翻译我需要的内容(不是实际代码,对不起,我正在努力)。我看了很多堆栈溢出问题和答案,但我似乎无法找到完美的组合。我知道 rep 函数会重复,并且我可以使用 case_when 或 ifelse.

编写条件语句
df_1 <- df %>%
  repeat row case_when df$food==1 %>%
   mutate (the_first_row (df$weight=prior_w),
          second_row (df$food=0, df$prior_w = NA))

感谢您的帮助

我们可以使用uncount复制行,然后根据行数按组更改值

library(dplyr)
library(tidyr)
df %>% 
  uncount(1 + (food == 1)) %>% 
  group_by(date) %>%
  mutate(food = if(n() > 1) replace(food, -1, 0) else food,
          weight = if(n() == 2) replace(weight, 1, prior_w[n()]) else weight,
          prior_w = if(n() ==2 ) lag(prior_w) else prior_w) %>%
  ungroup

-输出

# A tibble: 6 × 4
  date        food prior_w weight
  <chr>      <dbl>   <dbl>  <dbl>
1 2022-01-01     0      NA      5
2 2022-01-02     0      NA      4
3 2022-01-03     0      NA      3
4 2022-01-04     1      NA      2
5 2022-01-04     0       2      6
6 2022-01-05     0      NA      4