2018-04-18 12:24:54 +02:00
|
|
|
context("freq.R")
|
|
|
|
|
|
|
|
test_that("frequency table works", {
|
2018-07-02 09:34:20 +02:00
|
|
|
expect_equal(nrow(freq(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5))), 5)
|
|
|
|
expect_equal(nrow(frequency_tbl(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5))), 5)
|
2018-04-18 12:24:54 +02:00
|
|
|
|
2018-07-25 14:17:04 +02:00
|
|
|
# date column of septic_patients should contain 1151 unique dates
|
|
|
|
expect_equal(nrow(freq(septic_patients$date)), 1151)
|
2018-07-02 09:34:20 +02:00
|
|
|
expect_equal(nrow(freq(septic_patients$date)),
|
2018-04-18 12:24:54 +02:00
|
|
|
length(unique(septic_patients$date)))
|
|
|
|
|
2018-07-09 14:02:58 +02:00
|
|
|
# character
|
|
|
|
expect_output(print(freq(septic_patients$bactid)))
|
|
|
|
# integer
|
2018-07-01 21:40:37 +02:00
|
|
|
expect_output(print(freq(septic_patients$age)))
|
2018-06-20 14:47:37 +02:00
|
|
|
# date
|
2018-07-01 21:40:37 +02:00
|
|
|
expect_output(print(freq(septic_patients$date)))
|
2018-06-20 14:47:37 +02:00
|
|
|
# factor
|
2018-07-01 21:40:37 +02:00
|
|
|
expect_output(print(freq(septic_patients$hospital_id)))
|
2018-07-09 14:02:58 +02:00
|
|
|
# table
|
2018-07-11 12:34:02 +02:00
|
|
|
expect_output(print(freq(table(septic_patients$sex, septic_patients$age))))
|
2018-04-18 12:24:54 +02:00
|
|
|
|
|
|
|
library(dplyr)
|
2018-07-01 21:40:37 +02:00
|
|
|
expect_output(septic_patients %>% select(1:2) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:3) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:4) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:5) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:6) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:7) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:8) %>% freq() %>% print())
|
|
|
|
expect_output(septic_patients %>% select(1:9) %>% freq() %>% print())
|
2018-06-20 14:47:37 +02:00
|
|
|
|
|
|
|
# top 5
|
|
|
|
expect_equal(
|
|
|
|
septic_patients %>%
|
2018-07-01 21:40:37 +02:00
|
|
|
freq(bactid) %>%
|
2018-06-20 14:47:37 +02:00
|
|
|
top_freq(5) %>%
|
|
|
|
length(),
|
|
|
|
5)
|
|
|
|
# there're more than 5 lowest values
|
|
|
|
expect_gt(
|
|
|
|
septic_patients %>%
|
2018-07-01 21:40:37 +02:00
|
|
|
freq(bactid) %>%
|
2018-06-20 14:47:37 +02:00
|
|
|
top_freq(-5) %>%
|
|
|
|
length(),
|
|
|
|
5)
|
|
|
|
# n has length > 1
|
|
|
|
expect_error(
|
|
|
|
septic_patients %>%
|
2018-07-01 21:40:37 +02:00
|
|
|
freq(bactid) %>%
|
2018-06-20 14:47:37 +02:00
|
|
|
top_freq(n = c(1, 2))
|
|
|
|
)
|
|
|
|
# input must be freq tbl
|
|
|
|
expect_error(septic_patients %>% top_freq(1))
|
|
|
|
|
2018-07-08 22:14:55 +02:00
|
|
|
# charts from plot and hist, should not raise errors
|
|
|
|
plot(freq(septic_patients, age))
|
|
|
|
hist(freq(septic_patients, age))
|
|
|
|
|
2018-07-09 14:02:58 +02:00
|
|
|
# check vector
|
|
|
|
expect_identical(septic_patients %>%
|
|
|
|
freq(age) %>%
|
|
|
|
as.vector() %>%
|
|
|
|
sort(),
|
|
|
|
septic_patients %>%
|
|
|
|
pull(age) %>%
|
|
|
|
sort())
|
|
|
|
|
2018-07-30 00:57:49 +02:00
|
|
|
# check format
|
|
|
|
expect_identical(septic_patients %>%
|
|
|
|
freq(age) %>%
|
|
|
|
format() %>%
|
|
|
|
apply(2, class) %>%
|
|
|
|
unname(),
|
|
|
|
rep("character", 5))
|
|
|
|
|
|
|
|
# check tibble
|
|
|
|
expect_identical(septic_patients %>%
|
|
|
|
freq(age) %>%
|
|
|
|
as_tibble() %>%
|
|
|
|
class() %>%
|
|
|
|
.[1],
|
|
|
|
"tbl_df")
|
2018-04-18 12:24:54 +02:00
|
|
|
})
|
|
|
|
|