尝试更改c列以考虑到d列中发生的更改。
“ Id”的“结果”为“ 0”或“ 1”,有些结果的“结果”都取决于星期,因此在current_df中将其标记为“ 2”。
我想根据“ week_of_change”列中的信息将其更改为“ 0”和“ 1”。
# current_df
id=c(1,1,1,2,2,2,3,3,3,4,4,4)
week=c(5,6,7,11,12,13,19,20,21,5,8,10)
outcome=c(0,0,0,2,2,2,2,2,2,1,1,1)
week_of_change=c(NA,NA,NA,13,13,13,20,20,20,NA,NA,NA)
df<-data.frame(id,week,outcome,week_of_change)
# new_df
new_outcome=c(0,0,0,0,0,1,0,1,1,1,1,1)
new_df<-data.frame(id,week,new_outcome,week_of_change)
答案 0 :(得分:0)
library(dplyr)
# new_df
new_df<-df %>%
mutate(outcome2 = if_else(week>= week_of_change,
1,0)) %>%
mutate(outcome = ifelse(outcome == 2,
outcome2, outcome)) %>%
select(-outcome2)