You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现cumsum()与TTR::runSum()的混合累加函数(无需NA处理)

A Clean Hybrid of cumsum() and TTR::runSum()

Great question! Instead of juggling ifelse and NA values, you can create a clean custom function that directly combines the behavior of cumsum() and TTR::runSum() without messy NA handling. Here's how to do it efficiently:

Custom Function Implementation

We can split the data into two logical parts to avoid NA processing entirely:

  • The first n-1 elements: use cumsum() since we don't have enough data for a full sliding window yet
  • From the nth element onward: use runSum() (which already calculates the sliding window sum correctly once the window is full)
library(TTR)

cum_run_sum <- function(x, n) {
  # Handle edge case where n=1 (switches to runSum immediately, which equals the original vector)
  if (n == 1) return(runSum(x, n))
  
  # Calculate cumulative sum for the first n-1 elements
  cumulative_part <- cumsum(x[1:(n-1)])
  # Extract valid sliding window values from runSum (skip leading NAs)
  sliding_part <- runSum(x, n)[n:length(x)]
  # Combine both parts into the final result
  c(cumulative_part, sliding_part)
}

Test It With Your Example

Let's verify this matches your desired result perfectly:

data <- rep(1:3, 2)

# Your original desired result
runSum_result <- runSum(data, n = 3)
DesiredResult <- ifelse(is.na(runSum_result), cumsum(data), runSum_result)

# Our custom function result
hybrid_result <- cum_run_sum(data, n = 3)

# Check equality
all.equal(hybrid_result, DesiredResult)
#> [1] TRUE

The output of cum_run_sum(data, 3) is [1] 1 3 6 6 9 6, which exactly matches your DesiredResult.

Why This Works Better

  • No NA handling required: We directly extract valid sliding window values from runSum() starting at index n, so we never have to interact with leading NA values.
  • Efficient: Leverages the optimized C-backed implementation of TTR::runSum() instead of slower row-wise operations (like loops or apply functions).
  • Clear logic: The function explicitly separates the two behaviors, making it easy to adjust the transition point or tweak logic later if needed.

Alternative (Using zoo for Flexibility)

If you prefer a more flexible approach (e.g., dynamic window adjustments), you could use zoo::rollapply, though it's less efficient for large datasets:

library(zoo)

cum_run_sum_zoo <- function(x, n) {
  rollapply(x, seq_along(x), function(window) {
    if (length(window) < n) sum(window) else sum(tail(window, n))
  })
}

For most use cases, the first custom function is the best balance of speed and readability.

内容的提问来源于stack exchange,提问作者Will T-E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:27:09