【问题标题】:Disaggregate data from yearly to monthly basis将数据从年度分解为月度
【发布时间】:2022-01-18 18:22:03
【问题描述】:

我有来自项目的数据,这些数据应该报告一定数量的年度节省。 我需要从计划开始的月份到 12 个月后的月份将数据从年度核算到每月核算。为简单起见,每个月的储蓄金额只是每年的储蓄除以 12。

我需要在 Tibco.Spotfire 或 R 中执行此操作。

例如:我需要从这里开始:

到这里:

【问题讨论】:

    标签: r date spotfire unpivot


    【解决方案1】:

    以下解决方案应该有效。它需要data.tablelubridate。在 Spotfire 中,您可以使用工具 -> TERR 工具 -> 包管理来安装这两者。在 Spotfire 中设置数据函数有很多资源 (https://datashoptalk.com/spotfire-data-functions-terr-basics/)

    library(lubridate)
    library(data.table)
    dt <- data.table(Initiative = c('A', 'B') ,
                     start_date = c(as.Date('2017/1/1'), as.Date('2015/5/1')),
                     Savings = c(240, 120)
                     )
    
    new_dt <- dt[, .( 
           Date = seq.Date(start_date, as.Date(start_date %m+% months(11)), by = 'month'),
           Monthly_Savings = Savings / 12), by = Initiative]
    
    new_dt
    
        Initiative       Date Monthly_Savings
     1:          A 2017-01-01              20
     2:          A 2017-02-01              20
     3:          A 2017-03-01              20
     4:          A 2017-04-01              20
     5:          A 2017-05-01              20
     6:          A 2017-06-01              20
     7:          A 2017-07-01              20
     8:          A 2017-08-01              20
     9:          A 2017-09-01              20
    10:          A 2017-10-01              20
    11:          A 2017-11-01              20
    12:          A 2017-12-01              20
    13:          B 2015-05-01              10
    14:          B 2015-06-01              10
    

    【讨论】:

      【解决方案2】:

      这是 R 中使用 tidyverse 的另一种解决方案:

      library(dplyr, warn.conflicts = FALSE)
      library(lubridate, warn.conflicts = FALSE)
      
      # Creating your data ------------------------------------------------------
      
      data <- tibble(
        initiative = c("A", "B"),
        starting_date = c("may-18", "jun-17"),
        savings = c(240, 120)
      ) 
      
      data
      #> # A tibble: 2 x 3
      #>   initiative starting_date savings
      #>   <chr>      <chr>           <dbl>
      #> 1 A          may-18            240
      #> 2 B          jun-17            120
      
      # Formatting starting_date as date ----------------------------------------
      
      data <- 
        data %>% 
        mutate(
          starting_date = my(starting_date)
        )
      
      data
      #> # A tibble: 2 x 3
      #>   initiative starting_date savings
      #>   <chr>      <date>          <dbl>
      #> 1 A          2018-05-01        240
      #> 2 B          2017-06-01        120
      
      # Disaggregate ------------------------------------------------------------
      
      data <- 
        data %>% 
        group_by(initiative) %>%
        summarise(
          initiative = rep(initiative, 12),
          date = seq.Date(
            from = starting_date, 
            to = starting_date %m+% months(11), 
            by = "month"
          ),
          monthly_savings = rep(savings/12)
        ) %>% 
        ungroup() 
      #> `summarise()` has grouped output by 'initiative'. You can override using the `.groups` argument.
      
      data %>% 
        print(n = Inf)
      #> # A tibble: 24 x 3
      #>    initiative date       monthly_savings
      #>    <chr>      <date>               <dbl>
      #>  1 A          2018-05-01              20
      #>  2 A          2018-06-01              20
      #>  3 A          2018-07-01              20
      #>  4 A          2018-08-01              20
      #>  5 A          2018-09-01              20
      #>  6 A          2018-10-01              20
      #>  7 A          2018-11-01              20
      #>  8 A          2018-12-01              20
      #>  9 A          2019-01-01              20
      #> 10 A          2019-02-01              20
      #> 11 A          2019-03-01              20
      #> 12 A          2019-04-01              20
      #> 13 B          2017-06-01              10
      #> 14 B          2017-07-01              10
      #> 15 B          2017-08-01              10
      #> 16 B          2017-09-01              10
      #> 17 B          2017-10-01              10
      #> 18 B          2017-11-01              10
      #> 19 B          2017-12-01              10
      #> 20 B          2018-01-01              10
      #> 21 B          2018-02-01              10
      #> 22 B          2018-03-01              10
      #> 23 B          2018-04-01              10
      #> 24 B          2018-05-01              10
      

      我已将您的日期保留为 yyyy.mm.dd,但您可以将它们恢复为使用 data %&gt;% mutate(date = format(date, "%b-%y")) 的格式。

      【讨论】:

        猜你喜欢
        • 2021-03-08
        • 2023-03-31
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2020-11-16
        • 2021-01-24
        相关资源
        最近更新 更多