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 median #260

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

cumulative median #260

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 Median
#' 
#' @family Vector Function
#' 
#' @author Steven P. Sanderson II, MPH
#' 
#' @details
#' A function to return the cumulative median of a vector.
#' 
#' @description 
#' A function to return the cumulative median of a vector.
#' 
#' @parm .x A numeric vector
#' 
#' @examples 
#' x <- mtcars$mpg
#' 
#' cmedian(x)
#' 
#' @return 
#' A numeric vector
#' 
#' @export
#' 

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

Example:

> cmedian(x)
 [1] 21.00 21.00 21.00 21.20 21.00 21.00 21.00 21.00 21.00 21.00 21.00 20.10 19.20
[14] 18.95 18.70 18.40 18.10 18.40 18.70 18.95 19.20 18.95 18.70 18.40 18.70 18.95
[27] 19.20 19.20 19.20 19.20 19.20 19.20
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