【问题标题】:R resetting a cumsum to zero at the start of each yearR 在每年年初将 cumsum 重置为零
【发布时间】:2011-12-16 15:43:42
【问题描述】:

我有一个dataframe,里面有一堆捐款数据。我把数据从最旧的到最近的礼物按时间顺序排列。接下来,我添加一列,其中包含随着时间的推移累积的礼物总和。该数据包含多年的数据,我一直在寻找一种在每年年初将cumsum 重置为 0 的好方法(出于财务目的,该年从 7 月 1 日开始和结束)。

目前是这样的:

id        date          giftamt      cumsum()
005       01-05-2001     20.00        20.00
007       06-05-2001     25.00        45.00
009       12-05-2001     20.00        65.00
012       02-05-2002     30.00        95.00
015       08-05-2002     50.00       145.00
025       12-05-2002     25.00       170.00
...          ...          ...         ...

这是我想要的样子:

id        date          giftamt      cumsum()
005       01-05-2001     20.00        20.00
007       06-05-2001     25.00        45.00
009       12-05-2001     20.00        20.00
012       02-05-2002     30.00        50.00
015       08-05-2002     50.00        50.00
025       12-05-2002     25.00        75.00
...          ...          ...          ...

有什么建议吗?

更新:

这是最终由 Seb 提供的代码:

#tweak for changing the calendar year to fiscal year
df$year <- as.numeric(format(as.Date(df$giftdate), format="%Y"))
df$month <- as.numeric(format(as.Date(df$giftdate), format="%m"))
df$year <- ifelse(df$month<=6, df$year, df$year+1)

#cum-summing :)
library(plyr)
finalDf <- ddply(df, .(year), summarize, cumsum(as.numeric(as.character(giftamt))))

【问题讨论】:

  • 您的示例输出是否应该有所不同,或者您是否希望总和在 2001 年 5 月 12 日重置?
  • 重置日期为 7 月 1 日。抱歉数据稀疏。
  • 我在最初的回答中对此进行了监督 - 抱歉。我的新尝试现在应该可以了。我认为...

标签: r plyr


【解决方案1】:

我会这样尝试(df 是数据框):

#tweak for changing the calendar year to fiscal year
df$year <- format(as.Date(df$date), format="%Y")
df$month <- format(as.Date(df$date), format="%m")
df$year <- ifelse(df$month<=6, year, year+1)

#cum-summing :)
library(plyr)
ddply(df, .(year), summarize, cumsum(giftamt))

【讨论】:

    【解决方案2】:

    有两个任务:在数据框中创建一个代表每年的列,然后拆分数据,应用 cumsum,然后重新组合。 R 有很多方法可以完成这两个部分。

    可能最易读的 dong 第一个任务是使用 lubridate 包中的 year

    library(lubridate)
    df$year <- year(df$date)
    

    请注意,R 有很多日期格式,因此值得检查一下您当前使用的是POSIXctDatechronzooxts 还是其他格式之一。

    Seb 的选择或ddply 是我推荐的第二个任务。为了完整起见,您还可以使用tapplyaggregate

    with(df, tapply(giftamt, year, cumsum))
    aggregate(giftamt ~ year, df, cumsum)
    

    有了您希望在 7 月 1 日更改年份的新信息,请将年份列更新为

    df$year <- with(df, year(date) + (month(date) >= 7))
    

    【讨论】:

      【解决方案3】:
      gifts <- read.table("gifts.txt", header=T, quote="\"")
      NbGifts <- nrow(gifts)
      
      # Determination of the relevant fiscal year ending dates
      CalYear <- as.numeric(substr(gifts$date,7,10)) # calendar years
      TCY <- as.numeric(names(table(CalYear))) # list of calendar years
      MDFY <- "07-01-" # ending date for the current fiscal year
      EFY <- paste(MDFY,TCY,sep="") # list of fiscal year ending dates
      EFYplus <- cbind(TCY,EFY) # table of fiscal year ending dates
      colnames(EFYplus) <- c("CalYear","EndDate")
      
      # Manipulation of data frames in order to match
      # the fiscal year end dates to the relevant dates
      giftsPlusYear <- data.frame(CalYear, gifts, stringsAsFactors = FALSE)
      giftsPlusEFY <- merge(giftsPlusYear,EFYplus) # using the CalYear
      
      # Date comparison in order to associate a gift to its fiscal year
      DateGift <- as.Date(giftsPlusEFY$date,"%m-%d-%y") # date conversion for comparison
      DateEFY <- as.Date(giftsPlusEFY$EndDate,"%m-%d-%y")
      FiscYear <- ifelse(DateGift<DateEFY,giftsPlusEFY$CalYear,giftsPlusEFY$CalYear+1)
      
      # Computation of cumulative totals per fiscal year
      LastFY <- 0
      CumGift <- rep(0,NbGifts)
      for (g in 1:NbGifts){
        if (LastFY==FiscYear[g]){
          CumGift[g] <- CumGift[g-1] + gifts$giftamt[g]
          } else {
            CumGift[g] <- gifts$giftamt[g]
            LastFY <- FiscYear[g]
          }
      }
      (CumGifts <- cbind(gifts,CumGift))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-29
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多