更新时间:2023-02-02 23:03:12
示例中的Person_Id
字段不匹配.
The Person_Id
fields in your examples don't match.
我不确定这是否是您要使用的,但是我已经从您的dput()
中创建了一个删除最后一列的文件:
I'm not sure if this is what you're after, but from your dput()
I have created a file that removes the last column:
df_input <- df_output %>%
select(-Preceding_visit_id)
然后执行以下操作:
df_input %>%
group_by(Person_Id) %>%
mutate(Preceding_visit_id = lag(Visit_Id))
输出为:
# A tibble: 14 x 4
# Groups: Person_Id [3]
Person_Id Visit_Id Purpose Preceding_visit_id
<dbl> <dbl> <chr> <dbl>
1 1 1 checkup NA
2 1 2 checkup 1
3 1 3 checkup 2
4 1 4 checkup 3
5 1 5 checkup 4
6 2 6 checkup NA
7 2 7 checkup 6
8 2 8 checkup 7
9 2 9 checkup 8
10 2 10 checkup 9
11 2 11 checkup 10
12 3 12 checkup NA
13 3 13 checkup 12
14 3 14 checkup 13