File: test-data_replicate.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
test_that("data_replicate: simple use case", {
  data(mtcars)
  d <- head(mtcars)
  out <- data_replicate(d, "carb")
  expect_identical(dim(out), c(13L, 10L))
  expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 108, 258, 360, 360, 225))
  expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

  out <- data_replicate(d, 11)
  expect_identical(dim(out), c(13L, 10L))
  expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 108, 258, 360, 360, 225))
  expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

  d$mpg[5] <- NA
  out <- data_replicate(d, "carb")
  expect_identical(dim(out), c(13L, 10L))
  expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 22.8, 21.4, NA, NA, 18.1))
  expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

  d$carb[3] <- NA
  out <- data_replicate(d, "carb", remove_na = TRUE)
  expect_identical(dim(out), c(12L, 10L))
  expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 21.4, NA, NA, 18.1))
  expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

  out <- data_replicate(d, "carb", select = c("disp", "hp"), remove_na = TRUE)
  expect_identical(dim(out), c(12L, 2L))
  expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 258, 360, 360, 225))
  expect_named(out, c("disp", "hp"))

  d <- data.frame(
    a = c("a", "b", "c"),
    b = 1:3,
    rep = c(3, 2, 4),
    stringsAsFactors = FALSE
  )
  out <- data_replicate(d, "rep")
  expect_identical(out$a, c("a", "a", "a", "b", "b", "c", "c", "c", "c"))
})


test_that("data_replicate: errors", {
  data(mtcars)
  d <- head(mtcars)
  expect_error(data_replicate(d), regex = "No column")
  expect_error(data_replicate(d, expand = c("mpg", "gear")), regex = "a single string")
  expect_error(data_replicate(d, expand = "geas"), regex = "The column provided")
  expect_error(data_replicate(d, expand = "qsec"), regex = "The column provided")
  d$carb[3] <- NA
  expect_error(data_replicate(d, "carb"), regex = "missing values")
  d <- head(mtcars)
  d$carb[3] <- Inf
  expect_error(data_replicate(d, "carb"), regex = "infinite values")
})