File: test-data_duplicated.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 (112 lines) | stat: -rw-r--r-- 2,045 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Preparations

df1 <- data.frame(
  id = c(1, 2, 3, 1, 3),
  year = c(2022, 2022, 2022, 2022, 2000),
  item1 = c(NA, 1, 1, 2, 3),
  item2 = c(NA, 1, 1, 2, 3),
  item3 = c(NA, 1, 1, 2, 3)
)

expected1 <- data.frame(
  Row = c(1, 4, 3, 5),
  id = c(1, 1, 3, 3),
  year = c(2022, 2022, 2022, 2000),
  item1 = c(NA, 2, 1, 3),
  item2 = c(NA, 2, 1, 3),
  item3 = c(NA, 2, 1, 3),
  count_na = c(3, 0, 0, 0)
)

expected2 <- data.frame(
  Row = c(1, 4),
  id = c(1, 1),
  year = c(2022, 2022),
  item1 = c(NA, 2),
  item2 = c(NA, 2),
  item3 = c(NA, 2),
  count_na = c(3, 0)
)

# Testing

test_that("data_duplicated basic", {
  x <- data_duplicated(df1, select = "id")
  rownames(x) <- NULL
  expect_equal(
    x,
    expected1
  )
})

test_that("data_duplicated unquoted", {
  x <- data_duplicated(df1, select = id)
  rownames(x) <- NULL
  expect_equal(
    x,
    expected1
  )
})

test_that("data_duplicated vector", {
  x <- data_duplicated(df1, select = 1)
  rownames(x) <- NULL
  expect_equal(
    x,
    expected1
  )
})

test_that("data_duplicated select-helper", {
  x <- data_duplicated(df1, select = contains("id"))
  rownames(x) <- NULL
  expect_equal(
    x,
    expected1
  )
})

test_that("data_duplicated multiple IDs", {
  x <- data_duplicated(df1, select = c("id", "year"))
  rownames(x) <- NULL
  expect_equal(
    x,
    expected2
  )
})

test_that("data_duplicated multiple IDs formula", {
  x <- data_duplicated(df1, select = ~ id + year)
  rownames(x) <- NULL
  expect_equal(
    x,
    expected2
  )
})

test_that("data_duplicated multiple IDs vector", {
  x <- data_duplicated(df1, select = 1:2)
  rownames(x) <- NULL
  expect_equal(
    x,
    expected2
  )
})

test_that("data_unique works with groups", {
  df <- data.frame(
    g = c(1, 1, 2, 2),
    x = c(1, 1, 2, 1)
  )
  df <- data_group(df, "g")

  expected <- data.frame(
    Row = 1:2,
    g = c(1, 1),
    x = c(1, 1),
    count_na = c(0, 0)
  )
  expected <- data_group(expected, "g")

  expect_identical(data_duplicated(df, "x"), expected, ignore_attr = TRUE)
})