Cumulative standard deviation: Difference between revisions

Content added Content deleted
Line 3,574: Line 3,574:
If the goal is to get a vector of running standard deviations, the simplest is to do it with cumsum:
If the goal is to get a vector of running standard deviations, the simplest is to do it with cumsum:


<lang r>cumsd <- function(x) {
<lang rsplus>cumsd <- function(x) {
n <- seq_along(x)
n <- seq_along(x)
sqrt(cumsum(x^2) / n - (cumsum(x) / n)^2)
sqrt(cumsum(x^2) / n - (cumsum(x) / n)^2)
Line 3,592: Line 3,592:
The task requires an accumulator solution:
The task requires an accumulator solution:


<lang r>accumsd <- function() {
<lang rsplus>accumsd <- function() {
n <- 0
n <- 0
m <- 0
m <- 0