6 将持续天数转换成每天出现频次
题目6: 有数据框:
date_df <- data.frame(start = c("06-01", "06-02", "06-03", "06-04", "06-04"),
duration = c(4, 3, 1, 1, 0), stringsAsFactors=FALSE)
start
列是日期,duration
列是从该日期天开始持续的天数。我想最后求得每天出现的频次是多少。
N 版
# 识别日期:
date_df$ystart <- as.Date(
paste0('2019-', date_df$start))
# 生成所有日期:
date_ls <- mapply(
function(x1, x2)
format(
seq(from = x1, length.out = x2, by = 1),
'%m-%d'),
date_df$ystart, date_df$duration)
# 统计频次:
table(unlist(date_ls))
##
## 06-01 06-02 06-03 06-04
## 1 2 3 3
T 版
library(lubridate)
get_series = function(start, duration){
start= as_datetime(start, format="%m-%d")
end = start + days(duration)
return(seq(start, end, by="1 day"))
}
rslt = lapply(1:nrow(date_df), function(i){
rslt = get_series(date_df$start[i],
date_df$duration[i])
rslt=as.character(rslt)
return(rslt)
})
table(unlist(rslt))
##
## 2020-06-01 2020-06-02 2020-06-03
## 1 2 3
## 2020-06-04 2020-06-05
## 5 3