AMR/tests/testthat/test-freq.R

57 lines
1.7 KiB
R
Raw Normal View History

context("freq.R")
test_that("frequency table works", {
2018-05-09 11:44:46 +02:00
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
2018-05-09 11:44:46 +02:00
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)))
2018-06-20 14:47:37 +02:00
# int
expect_output(freq(septic_patients$age))
2018-06-20 14:47:37 +02:00
# date
expect_output(freq(septic_patients$date))
2018-06-20 14:47:37 +02:00
# factor
expect_output(freq(septic_patients$hospital_id))
library(dplyr)
expect_output(septic_patients %>% select(1:2) %>% freq())
expect_output(septic_patients %>% select(1:3) %>% freq())
expect_output(septic_patients %>% select(1:4) %>% freq())
expect_output(septic_patients %>% select(1:5) %>% freq())
expect_output(septic_patients %>% select(1:6) %>% freq())
expect_output(septic_patients %>% select(1:7) %>% freq())
expect_output(septic_patients %>% select(1:8) %>% freq())
expect_output(septic_patients %>% select(1:9) %>% freq())
2018-06-20 14:47:37 +02:00
# top 5
expect_equal(
septic_patients %>%
select(bactid) %>%
freq(as.data.frame = TRUE) %>%
top_freq(5) %>%
length(),
5)
# there're more than 5 lowest values
expect_gt(
septic_patients %>%
select(bactid) %>%
freq(as.data.frame = TRUE) %>%
top_freq(-5) %>%
length(),
5)
# n has length > 1
expect_error(
septic_patients %>%
select(bactid) %>%
freq(as.data.frame = TRUE) %>%
top_freq(n = c(1, 2))
)
# input must be freq tbl
expect_error(septic_patients %>% top_freq(1))
})