R整理数据技巧
根据姓名和日期排序后,给一列排序号
df %>%
arrange(姓名, 日期) %>%
group_by(姓名) %>%
mutate(record_rank = row_number()) %>%
dplyr::ungroup()
如果事件1出现后,后面所有的值用NA填充
针对在进行整理生存分析数据时
df %>%
arrange(姓名, record_rank) %>%
group_by(姓名) %>%
mutate(mark = cumsum(status == 1)) %>%
mutate(new_status = if_else(mark <= 1, status, NA)) %>%
ungroup() %>%
mutate(new_status = ifelse(is.na(new_status), 0, new_status))
向上或向下填充NA值
df %>%
group_by(姓名) %>%
fill(feature1, .direction = 'up') %>%
fill(feature1, .direction = 'down')