-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from ecohealthalliance/fix/correct_data_types
Fix/correct data types
- Loading branch information
Showing
9 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#' Class to Column Type lookup table | ||
#' | ||
#' A table that links classes to `readr` column types. | ||
#' Created from csv file of the same name in inst/ | ||
#' | ||
#' class_to_col_type <- read.csv(file = "inst/class_to_col_type.csv") | ||
#' usethis::use_data(class_to_col_type,overwrite = TRUE) | ||
#' | ||
#' @format ## `class_to_col_type` | ||
#' A data frame with 9 rows and 3 columns: | ||
#' \describe{ | ||
#' \item{col_type}{Type of column as described in `readr`} | ||
#' \item{col_class}{Class of R object that matches that column type} | ||
#' \item{col_abv}{Abbreviation for that column type from `reader`} | ||
#' ... | ||
#' } | ||
#' @seealso [reader::cols()] | ||
"class_to_col_type" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' Guess the column type | ||
#' | ||
#' uses column class to set readr column type | ||
#' | ||
#' @param data data.frame Data who column types you would like to guess | ||
#' @param default_col_abv string. Column type abbreviation from [readr::cols()]. | ||
#' Use "g" to guess the column type. | ||
#' | ||
#' @return character vector of column abbreviations | ||
#' @export | ||
#' | ||
#' @examples | ||
#' data <- data.frame(time = Sys.time(), | ||
#' char = "hello", num = 1, log = TRUE, | ||
#' date = Sys.Date(), list_col = list("hello") ) | ||
#' | ||
#' guess_col_type(data) | ||
#' | ||
#' ## change default value of default column abbreviation | ||
#' | ||
#' guess_col_type(data, default_col_abv = "g") | ||
#' | ||
#' | ||
guess_col_type <- function(data, default_col_abv = "c"){ | ||
|
||
## get col type abbreviations | ||
purrr::map_chr(data, function(x){ | ||
class_x <- data.frame(col_class = class(x)) | ||
|
||
# class_to_col_type is created from class_to_col_type.csv in the inst folder | ||
class_df <- dplyr::left_join(class_x,ohcleandat::class_to_col_type,"col_class") | ||
|
||
col_abv <- dplyr::distinct(class_df,col_abv ) |> | ||
dplyr::pull(col_abv) | ||
|
||
na_check <- is.na(col_abv) | ||
|
||
if(all(na_check)){ | ||
class_for_msg <- paste(class(x),collapse = ", ") | ||
msg <- sprintf("column type %s might not be supported, defaulting to %s", class_for_msg,default_col_abv) | ||
rlang::warn(msg) | ||
return(default_col_abv) | ||
} | ||
|
||
if(any(na_check)){ | ||
# use non na class(es) | ||
col_abv <- col_abv[which(!na_check)] | ||
} | ||
|
||
if(length(col_abv) > 1){ | ||
rlang::warn("column has multiple classes of distinct types, | ||
using first class as column type") | ||
col_abv <- col_abv[1] | ||
} | ||
|
||
return(col_abv) | ||
}) | ||
|
||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
col_type,col_class,col_abv | ||
col_time,POSIXct,T | ||
col_time,POSIXt,T | ||
col_character,character,c | ||
col_number,numeric,n | ||
col_logical,logical,l | ||
col_date,Date,D | ||
col_factor,factor,f | ||
col_double,double,d | ||
col_integer,integer,i |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.