tests/testthat/test-describe.R

context("test-describe.R")

test_that("describe default", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- data.frame(gftools::describe(dat))
  df <- data.frame(
    1:2, c(10, 10), c(5.5, 1.5), c(3.02765035409749, 0.52704627669473),
    c(0.55048188256318, 0.351364184463153), c(1, 1), c(10, 2), c(9, 1),
    c(0.957427107756338, 0.166666666666667), c(55, 15)
  )
  colnames(df) <- names(test)
  rownames(test) <- NULL
  rownames(df) <- NULL
  expect_equal(test, df)
})

test_that("describe with IQR", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- data.frame(gftools::describe(dat, IQR = TRUE))
  df <- data.frame(
    1:2, c(10, 10), c(5.5, 1.5), c(3.02765035409749, 0.52704627669473),
    c(0.55048188256318, 0.351364184463153), c(1, 1), c(10, 2), c(9, 1),
    c(0.957427107756338, 0.166666666666667), c(55, 15), c(4.5, 1)
  )
  colnames(df) <- names(test)
  rownames(test) <- NULL
  rownames(df) <- NULL
  expect_equal(test, df)
})

test_that("describe with fast", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- data.frame(gftools::describe(dat, fast = TRUE))
  df <- data.frame(
    1:2, c(10, 10), c(5.5, NaN), c(3.02765035409749, NA), c(0.55048188256318, NA),
    c(1, Inf), c(10, -Inf), c(9, -Inf), c(0.957427107756338, NA), c(55, 0)
  )
  colnames(df) <- names(test)
  rownames(test) <- NULL
  rownames(df) <- NULL
  expect_equal(test, df)
})

test_that("describe with quant", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- data.frame(gftools::describe(dat, quant = c(0.1, 0.9)))
  df <- data.frame(
    1:2, c(10, 10), c(5.5, 1.5), c(3.02765035409749, 0.52704627669473),
    c(0.55048188256318, 0.351364184463153), c(1, 1), c(10, 2), c(9, 1),
    c(0.957427107756338, 0.166666666666667), c(55, 15), c(1.9, 1), c(9.1, 2)
  )
  colnames(df) <- names(test)
  rownames(test) <- NULL
  rownames(df) <- NULL
  expect_equal(test, df)
})

test_that("describeBy avec grp", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- gftools::describeBy(dat, group = "grp")
  testA <- data.frame(test$A)
  testB <- data.frame(test$B)
  dfA <- data.frame(1:2, c(5, 5), c(3, 1), c(1.58113883008419, 0), c(0.52704627669473, 0), c(1, 1), c(5, 1), c(4, 0), c(0.707106781186548, 0), c(15, 5))
  dfB <- data.frame(1:2, c(5, 5), c(8, 2), c(1.58113883008419, 0), c(0.197642353760524, 0), c(6, 2), c(10, 2), c(4, 0), c(0.707106781186548, 0), c(40, 10))
  colnames(dfA) <- names(testA)
  colnames(dfB) <- names(testB)
  rownames(testA) <- NULL
  rownames(dfA) <- NULL
  rownames(testB) <- NULL
  rownames(dfB) <- NULL
  expect_equal(testA, dfA)
  expect_equal(testB, dfB)
})

test_that("describeData", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- gftools::describeData(dat)
  expect_equal(test$n.obs, 10)
})

test_that("describeBy avec 'grp' et 'mat'=TRUE", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  test <- gftools::describeBy(dat, group = "grp", mat = TRUE) %>%
    dplyr::mutate_all(as.character)
  df <- data.frame(
    1:4, c("A", "B", "A", "B"), rep(x = 1:2, each = 2), rep(x = 5, each = 4), c(3, 8, 1, 2),
    c(1.58113883008419, 1.58113883008419, 0, 0), c(0.52704627669473, 0.197642353760524, 0, 0),
    c(1, 6, 1, 2), c(5, 10, 1, 2), c(4, 4, 0, 0), c(0.707106781186548, 0.707106781186548, 0, 0), c(15, 40, 5, 10)
  ) %>%
    dplyr::mutate_all(as.character)
  colnames(df) <- names(test)
  rownames(test) <- NULL
  rownames(df) <- NULL
  expect_equal(test, df)
})

test_that("describeBy grp = NULL", {
  dat <- data.frame(c(seq(from = 1, to = 10, by = 1)), c(rep(LETTERS[1:2], each = 5)))
  colnames(dat) <- c("valeur", "grp")
  expect_warning(gftools::describeBy(dat), "no grouping variable")
})
pobsteta/gftools documentation built on March 28, 2020, 8:25 p.m.