tests/testthat/test.freq.r

#library(testthat)
library(R3port)
context("frequency calculations")

test_that("frequencies correctly calculated", {
  tst <- data.frame(a=rep(1:3,4),b=sample(1:6,12,replace=TRUE))
  res1 <- freq(tst,"b")
  expect_equal(round(sum(as.numeric(res1$Perc)),1),100)
  expect_equal(sum(as.numeric(res1$Freq)),nrow(tst))
  expect_equal(unique(as.numeric(res1$b)),sort(unique(tst$b)))
  
  res2 <- freq(tst,"b",total="")
  expect_true(any(grepl("Total",res2$b)))
  expect_equal(round(as.numeric(res2$Perc[res2$b=="Total"]),1),100)
  expect_equal(as.numeric(res2$dnm[res2$b=="Total"]),nrow(tst))
  
  res3 <- freq(tst,"b",total="",denom=10,totaldenom = 20)
  expect_equal(as.numeric(res3$dnm[res3$b=="Total"]),20)
  expect_equal(unique(as.numeric(res3$dnm[res3$b!="Total"])),10)
})

Try the R3port package in your browser

Any scripts or data that you put into this service are public.

R3port documentation built on Sept. 22, 2023, 9:06 a.m.