R语言 实现SAS中的first.和last.
library(dplyr)
# subject 受试者编号 visit 访视周期 dose 给药剂量
data <- data.frame(subject = c("01001","01001","01001","01002","01002","01002","01003","01003","01003"),visit = c(1,2,3,3,2,1,3,2,1),dose = c(20,20,20,30,30,30,40,40,40))
#first.
data_first <- data %>% arrange(subject,visit) %>% group_by(subject) %>% slice(1)
#last.
data_last <- data %>% arrange(subject,visit) %>% group_by(subject) %>% slice(n())