diff --git a/NEWS.md b/NEWS.md index d2107421..c3eaf75a 100755 --- a/NEWS.md +++ b/NEWS.md @@ -75,6 +75,7 @@ * Added possibility to set any parameter to `geom_rsi` (and `ggplot_rsi`) so you can set your own preferences * Fix for joins, where predefined suffices would not be honoured * Added parameter `quote` to the `freq` function +* Added longest en shortest character length in the frequency table (`freq`) header of class `character` * Support for types (classes) list and matrix for `freq` ```r my_matrix = with(septic_patients, matrix(c(age, sex), ncol = 2)) diff --git a/R/freq.R b/R/freq.R index 978fb47d..28b3e1fe 100755 --- a/R/freq.R +++ b/R/freq.R @@ -315,6 +315,9 @@ frequency_tbl <- function(x, header <- header %>% paste0(markdown_line, 'Columns: ', mult.columns) } else { header <- header %>% paste0(markdown_line, 'Class: ', class(x) %>% rev() %>% paste(collapse = " > ")) + if (!mode(x) %in% class(x)) { + header <- header %>% paste0(" (", mode(x), ")") + } } header <- header %>% paste0(markdown_line, '\nLength: ', (NAs %>% length() + x %>% length()) %>% format(), @@ -322,6 +325,12 @@ frequency_tbl <- function(x, ' = ', (NAs %>% length() / (NAs %>% length() + x %>% length())) %>% percent(force_zero = TRUE, round = digits) %>% sub('NaN', '0', ., fixed = TRUE), ')') header <- header %>% paste0(markdown_line, '\nUnique: ', x %>% n_distinct() %>% format()) + if (NROW(x) > 0 & any(class(x) == "character")) { + header <- header %>% paste0('\n') + header <- header %>% paste0(markdown_line, '\nShortest: ', x %>% base::nchar() %>% base::min(na.rm = TRUE)) + header <- header %>% paste0(markdown_line, '\nLongest: ', x %>% base::nchar() %>% base::max(na.rm = TRUE)) + } + if (NROW(x) > 0 & any(class(x) %in% c('double', 'integer', 'numeric', 'raw', 'single'))) { # right align number Tukey_five <- stats::fivenum(x, na.rm = TRUE) diff --git a/tests/testthat/test-freq.R b/tests/testthat/test-freq.R index 4e747011..38918042 100755 --- a/tests/testthat/test-freq.R +++ b/tests/testthat/test-freq.R @@ -24,6 +24,7 @@ test_that("frequency table works", { # character expect_output(print(freq(septic_patients$mo))) + expect_output(print(freq(microorganisms$fullname))) # integer expect_output(print(freq(septic_patients$age))) # date @@ -61,7 +62,7 @@ test_that("frequency table works", { top_freq(5) %>% length(), 5) - # there're more than 5 lowest values + # there are more than 5 lowest values expect_gt( septic_patients %>% freq(mo) %>%