创建最低日期和最大基于季度,月份,年初至今的日期列

时间:2020-05-08 22:24:26

标签: r date

我有一个如下数据框:

Frequency  Period   Period No.  Year
Monthly    1        1           2018
Quarterly  Q1       3           2018
YTD        YTD-Feb  2           2019    

基于这些列,我想添加一个分钟。日期和最大日期列,以便数据框如下所示:

Frequency  Period   Period No.  Year  Min. Date  Max. Date
Monthly    1        1           2018  1/1/2018   1/31/2018
Quarterly  Q1       3           2018  1/1/2018   3/31/2018
YTD        YTD-Feb  2           2019  1/1/2019   2/28/2019

1 个答案:

答案 0 :(得分:0)

如果我们需要最大值,则基于'PeriodNo。'的最小值。列,从“年份”列中按月创建一系列日期,然后提取min和最大值`

library(dplyr)
library(purrr)
library(lubridate)
library(stringr)
df1 %>% 
   mutate(date = map2(as.Date(str_c(Year, '-01-01')), 
   PeriodNo., ~ seq(.x, length.out = .y, by = '1 month')), 
   Min.Date =   do.call(c, map(date, min)), 
   Max.Date = do.call(c, map(date, ~ceiling_date(max(.x), 'month')-1))) %>% 
   select(-date)
#  Frequency  Period PeriodNo. Year   Min.Date   Max.Date
#1   Monthly       1         1 2018 2018-01-01 2018-01-31
#2 Quarterly      Q1         3 2018 2018-01-01 2018-03-31
#3       YTD YTD-Feb         2 2019 2019-01-01 2019-02-28

或带有Map

的选项
lst1 <- Map(function(x, y) seq(as.Date(paste0(x, "-01-01")), 
    length.out = y, by = '1 month'), df1$Year, df1$PeriodNo.)
df1$Min.Date <- do.call(c, lapply(lst1, min))
df1$Max.Date <- do.call(c, lapply(lst1, function(x) (max(x) + months(1) -1)) )

数据

df1 <- structure(list(Frequency = c("Monthly", "Quarterly", "YTD"), 
    Period = c("1", "Q1", "YTD-Feb"), PeriodNo. = c(1L, 3L, 2L
    ), Year = c(2018L, 2018L, 2019L)), class = "data.frame",
    row.names = c(NA, 
-3L))