我想生成一种药物的结束日期,其中将结束日期定义为未注册该药物的第一个日期。 我有以下三种药物,a,b,c以及处方药可以注册的日期
Drug <- c("a","b","a","b","c","a","c","a","c","c")
Date <- c("2018-09-20","2018-09-20" , "2018-09-21","2018-09-21","2018-09-21",
"2018-09-22" ,"2018-09-22" ,"2018-09-23","2018-09-23",
"2018-09-24")
Dates <- cbind(Drug,Date)
Drug Date
[1,] "a" "2018-09-20"
[2,] "b" "2018-09-20"
[3,] "a" "2018-09-21"
[4,] "b" "2018-09-21"
[5,] "c" "2018-09-21"
[6,] "a" "2018-09-22"
[7,] "c" "2018-09-22"
[8,] "a" "2018-09-23"
[9,] "c" "2018-09-23"
[10,] "c" "2018-09-24"
我希望我的决赛桌看起来像这样:
end <- c("2018-09-24","2018-09-22" ,"2018-09-24","2018-09-22" ,NA,"2018-09-24",
NA,"2018-09-24",NA,NA)
Dates <- cbind(Dates,end)
Drug res end
"a" "2018-09-20" "2018-09-24"
"b" "2018-09-20" "2018-09-22"
"a" "2018-09-21" "2018-09-24"
"b" "2018-09-21" "2018-09-22"
"c" "2018-09-21" NA
"a" "2018-09-22" "2018-09-24"
"c" "2018-09-22" NA
"a" "2018-09-23" "2018-09-24"
"c" "2018-09-23" NA
"c" "2018-09-24" NA
答案 0 :(得分:0)
首先,我将您的数据集转换为数据框,以使多种操作变得更加容易。我将创建一个“帮助程序”数据框来存储每种药物的唯一终止日期,然后将这些日期添加到原始数据集中。
一路走来,我还将创建一个实用工具功能,该功能可以找到给定日期之后的日期。 (根据所需解决方案中NA的出现,我假设当您说“未注册药物的第一个日期”时,您指的是日期列表中的第一个日期 ,而不仅仅是下一个日历日。)
library(dplyr)
Dates = as.data.frame(Dates, stringsAsFactors = FALSE)
next_date <- function(one_date, all_dates) {
all_dates <- sort(unique(all_dates))
return(all_dates[which(one_date == all_dates)[[1]] + 1])
}
end_dates <- group_by(Dates, Drug)
end_dates <- summarize(end_dates, end = next_date(max(Date), Dates$Date))
Dates = left_join(Dates, end_dates)