R/center_grand_mean.R

Defines functions center_grand_mean

Documented in center_grand_mean

#' Center with respect to grand mean
#'
#' This function will compute grand-mean-centered scores.
#' 
#' @param data A data.frame or a data.frame extension (e.g. a tibble).
#' @param cols Columns that need to be centered. See `dplyr::dplyr_tidy_select` for available options. 
#' @param keep_original default is `FALSE`. Set to `TRUE` to keep original columns
#' 
#' @return
#' An object of the same type as .data. The output has the following properties:  
#' 1. Columns from .data will be preserved  
#' 2. Columns with scores that are grand-mean-centered.  
#' 
#' @export
#'
#' @examples
#' center_grand_mean(iris,where(is.numeric))
#'
center_grand_mean = function(data,cols,keep_original=TRUE) {
  cols = enquo(cols)
  original_df = data %>% dplyr::select(!!cols)
  return_df = data %>%
    dplyr::mutate(dplyr::across(!!cols, function(x) { (x - mean(x,na.rm = TRUE))})) %>% 
    dplyr::rename_with(~ paste(.,'_grand_c',sep = ''),!!cols)
  if (keep_original == TRUE) {
    return_df = dplyr::bind_cols(return_df,original_df)
  }
  return(return_df)
}

Try the psycCleaning package in your browser

Any scripts or data that you put into this service are public.

psycCleaning documentation built on Nov. 5, 2023, 9:06 a.m.