add shortest and longest to freq for characters

This commit is contained in:
dr. M.S. (Matthijs) Berends 2018-09-17 09:42:09 +02:00
parent db14781593
commit 7680e4edd8
3 changed files with 12 additions and 1 deletions

View File

@ -75,6 +75,7 @@
* Added possibility to set any parameter to `geom_rsi` (and `ggplot_rsi`) so you can set your own preferences * 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 * Fix for joins, where predefined suffices would not be honoured
* Added parameter `quote` to the `freq` function * 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` * Support for types (classes) list and matrix for `freq`
```r ```r
my_matrix = with(septic_patients, matrix(c(age, sex), ncol = 2)) my_matrix = with(septic_patients, matrix(c(age, sex), ncol = 2))

View File

@ -315,6 +315,9 @@ frequency_tbl <- function(x,
header <- header %>% paste0(markdown_line, 'Columns: ', mult.columns) header <- header %>% paste0(markdown_line, 'Columns: ', mult.columns)
} else { } else {
header <- header %>% paste0(markdown_line, 'Class: ', class(x) %>% rev() %>% paste(collapse = " > ")) 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(), 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), ')') ' = ', (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()) 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'))) { if (NROW(x) > 0 & any(class(x) %in% c('double', 'integer', 'numeric', 'raw', 'single'))) {
# right align number # right align number
Tukey_five <- stats::fivenum(x, na.rm = TRUE) Tukey_five <- stats::fivenum(x, na.rm = TRUE)

View File

@ -24,6 +24,7 @@ test_that("frequency table works", {
# character # character
expect_output(print(freq(septic_patients$mo))) expect_output(print(freq(septic_patients$mo)))
expect_output(print(freq(microorganisms$fullname)))
# integer # integer
expect_output(print(freq(septic_patients$age))) expect_output(print(freq(septic_patients$age)))
# date # date
@ -61,7 +62,7 @@ test_that("frequency table works", {
top_freq(5) %>% top_freq(5) %>%
length(), length(),
5) 5)
# there're more than 5 lowest values # there are more than 5 lowest values
expect_gt( expect_gt(
septic_patients %>% septic_patients %>%
freq(mo) %>% freq(mo) %>%