File: test-empty-dataframe.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 (74 lines) | stat: -rw-r--r-- 2,081 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
test_that("remove empty with character", {
  tmp <- data.frame(
    a = c(1, 2, 3, NA, 5),
    b = c(1, NA, 3, NA, 5),
    c = c(NA, NA, NA, NA, NA),
    d = c(1, NA, 3, NA, 5)
  )

  expect_identical(empty_columns(tmp), c(c = 3L))
  expect_identical(empty_rows(tmp), 4L)

  expect_identical(dim(remove_empty_columns(tmp)), c(5L, 3L))
  expect_identical(dim(remove_empty_rows(tmp)), c(4L, 4L))
  expect_identical(dim(remove_empty(tmp)), c(4L, 3L))

  expect_snapshot(remove_empty_columns(tmp))
  expect_snapshot(remove_empty_rows(tmp))
  expect_snapshot(remove_empty(tmp))
})


test_that("remove empty columns with character", {
  tmp <- data.frame(
    a = c(1, 2, 3, NA, 5),
    b = c("", NA, "", NA, ""),
    c = c(NA, NA, NA, NA, NA),
    d = c(1, NA, 3, NA, 5),
    e = c("", "", "", "", ""),
    stringsAsFactors = FALSE
  )

  expect_identical(empty_columns(tmp), c(b = 2L, c = 3L, e = 5L))
  expect_identical(dim(remove_empty_columns(tmp)), c(5L, 2L))
  expect_identical(dim(remove_empty(tmp)), c(4L, 2L))
})


test_that("remove empty rows with character", {
  tmp <- data.frame(
    a = c(1, "", 3, NA, 5),
    b = c("", NA, "", NA, ""),
    c = c(NA, NA, NA, NA, NA),
    d = c(1, NA, 3, NA, 5),
    e = c("", "", "", "", ""),
    f = factor(c("", "", "", "", "")),
    g = factor(c("", NA, "", NA, "")),
    stringsAsFactors = FALSE
  )

  expect_identical(empty_rows(tmp), c(2L, 4L))
  expect_identical(dim(remove_empty_rows(tmp)), c(3L, 7L))
  expect_identical(dim(remove_empty(tmp)), c(3L, 2L))
})

test_that("empty_columns with only NA characters", {
  tmp <- data.frame(
    var1 = c(1, 1, 1),
    var2 = c(NA_character_, NA_character_, NA_character_)
  )
  expect_identical(empty_columns(tmp), c(var2 = 2L))
})


test_that("works with non-ascii chars", {
  tmp <- data.frame(
    a = c(1, 2, 3, NA, 5),
    b = c("", NA, "", NA, ""),
    c = c(NA, NA, NA, NA, NA),
    d = c("test", "Se\x96ora", "works fine", "this too", "yeah"),
    e = c("", "", "", "", ""),
    stringsAsFactors = FALSE
  )
  expect_identical(empty_columns(tmp), c(b = 2L, c = 3L, e = 5L))
})