1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 10:06:12 +01:00
AMR/tests/testthat/test-freq.R

54 lines
1.7 KiB
R
Executable File

context("freq.R")
test_that("frequency table works", {
expect_equal(nrow(freq(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), as.data.frame = TRUE)), 5)
expect_equal(nrow(frequency_tbl(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), as.data.frame = TRUE)), 5)
# date column of septic_patients should contain 1662 unique dates
expect_equal(nrow(freq(septic_patients$date, as.data.frame = TRUE)), 1662)
expect_equal(nrow(freq(septic_patients$date, as.data.frame = TRUE)),
length(unique(septic_patients$date)))
# int
expect_output(print(freq(septic_patients$age)))
# date
expect_output(print(freq(septic_patients$date)))
# factor
expect_output(print(freq(septic_patients$hospital_id)))
library(dplyr)
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())
# top 5
expect_equal(
septic_patients %>%
freq(bactid) %>%
top_freq(5) %>%
length(),
5)
# there're more than 5 lowest values
expect_gt(
septic_patients %>%
freq(bactid) %>%
top_freq(-5) %>%
length(),
5)
# n has length > 1
expect_error(
septic_patients %>%
freq(bactid) %>%
top_freq(n = c(1, 2))
)
# input must be freq tbl
expect_error(septic_patients %>% top_freq(1))
})