Created
March 13, 2025 16:54
-
-
Save Lextuga007/9e24862df3d22bff80c656b2d6e5ea2f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #' Check for integers | |
| #' | |
| #' @param df data frame to check | |
| #' @param select_int data frame returns just the columns of data that are | |
| #' integer64. The default returns all columns. | |
| #' @param tidy logical set to TRUE to change set any integer or integer64 to | |
| #' integer and give a message. FALSE will return the original data with no changes | |
| #' and message. | |
| #' | |
| #' @returns data frame and log where integer64 found | |
| #' @export | |
| #' | |
| #' @examples | |
| #' \dontrun{ | |
| #' df <- tibble::tribble( | |
| #' ~colmA, ~colmB, | |
| #' 100, bit64::as.integer64(1) | |
| #' ) | |
| #' check_int64(df) | |
| #' } | |
| check_int64 <- function(df, | |
| select_int = FALSE, | |
| tidy = TRUE) { | |
| # Gets data type, pivots and filters to find integer64 | |
| int_cols <- df |> | |
| dplyr::summarise_all(class) |> | |
| tidyr::pivot_longer(cols = dplyr::everything()) |> | |
| dplyr::filter(value == "integer64") | |
| columns <- paste0(int_cols$name, collapse = ", ") | |
| if (select_int == FALSE | nrow(int_cols) == 0) { | |
| df | |
| } | |
| if (select_int == TRUE & nrow(int_cols) > 0) { | |
| cols <- int_cols$name |> | |
| dput() | |
| df <- df |> | |
| dplyr::select(dplyr::any_of(cols)) | |
| } | |
| if (select_int == TRUE & nrow(int_cols) == 0) { | |
| df | |
| } | |
| if (tidy == TRUE) { | |
| df <- df |> | |
| dplyr::mutate(dplyr::across(dplyr::where(is.numeric), as.numeric)) | |
| df | |
| } | |
| if (tidy == FALSE) { | |
| df | |
| } | |
| df | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tests (note that some of these relate to messages that were removed when I took out the loggit reference: