Last active
May 15, 2020 10:31
-
-
Save HanjoStudy/5118d3cf6d06132e7079cfd8b72f770d to your computer and use it in GitHub Desktop.
Util functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @noRd | |
#' @export | |
split_df <- function(df, number){ | |
n <- number | |
nr <- nrow(df) | |
split(df, rep(1:ceiling(nr/n), | |
each = n, length.out = nr)) | |
} | |
#' @noRd | |
#' @export | |
#' @importFrom glue glue | |
to_log <- function(x){ | |
cat(glue("[{timestamp(quiet = T)}] {x}"), "\n") | |
} | |
#' @noRd | |
#' @export | |
#' @importFrom digest digest | |
hash <- function(x){ | |
digest(x, serialize = FALSE, ascii = T, algo = "sha256") | |
} | |
#' @noRd | |
#' @export | |
clean_names <- function(dat, ...) { | |
stats::setNames(dat, to_snake_case(names(dat), ...)) | |
} | |
#' @noRd | |
#' @export | |
colors_n2l <- function(n){ | |
colorRampPalette(c("#8b0e3a", "#e79c29", "#104332"))(n) | |
} | |
#' @noRd | |
#' @export | |
compress_numbers <- function(tx) { | |
div <- findInterval(as.numeric(gsub("\\,", "", tx)), | |
c(0, 1e3, 1e6, 1e9, 1e12)) # modify this if negative numbers are possible | |
paste(round(as.numeric(gsub("\\,", "", tx)) / 10 ^ (3 * (div - 1)), 2), | |
c("", "K", "M", "B", "T")[div]) | |
} | |
#' takes summary() to tibble | |
#' @param x an integer | |
#' @keywords internal | |
#' | |
summary_to_df <- function(x){ | |
df <- summary(x) | |
tibble::tibble(type = c(names(df), "sd"), | |
value = c(df, sd(x))) %>% | |
tidyr::spread(type, value) %>% | |
janitor::clean_names() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment