Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cumulative standard deviation #265

Closed
Tracked by #250
spsanderson opened this issue Sep 19, 2022 · 0 comments · Fixed by #269
Closed
Tracked by #250

cumulative standard deviation #265

spsanderson opened this issue Sep 19, 2022 · 0 comments · Fixed by #269
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Sep 19, 2022

Function:

#' Cumulative Standard Deviation
#' 
#' @family Vector Function
#' 
#' @author Steven P. Sanderson II, MPH
#' 
#' @details
#' A function to return the cumulative standard deviation of a vector.
#' 
#' @description 
#' A function to return the cumulative standard deviation of a vector.
#' 
#' @parm .x A numeric vector
#' 
#' @examples 
#' x <- mtcars$mpg
#' 
#' csd(x)
#' 
#' @return 
#' A numeric vector
#' 
#' @export
#' 

csd <- function(.x){
  sapply(seq_along(.x), function(k, z) stats::sd(z[1:k]), z = .x)
}

Example:

x <- mtcars$mpg

> csd(x)
 [1]        NA 0.0000000 1.0392305 0.8544004 1.4737707 1.7663522 2.8445436
 [8] 3.1302385 3.0524580 2.9070986 2.8647069 2.9366416 2.8975233 3.0252418
[15] 3.7142967 4.1476098 4.1046423 5.2332053 5.7405452 6.4594362 6.3029736
[22] 6.2319940 6.1698105 6.1772007 6.0474457 6.1199296 6.1188444 6.3166405
[29] 6.2611772 6.1530527 6.1217574 6.0269481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

1 participant