File: test-data_remove.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 (130 lines) | stat: -rw-r--r-- 2,950 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
test_that("data_remove works as expected", {
  expect_identical(
    data_remove(BOD, "Time"),
    structure(list(demand = c(8.3, 10.3, 19, 16, 15.6, 19.8)),
      class = "data.frame",
      row.names = c(NA, 6L),
      reference = "A1.4, p. 270"
    )
  )
})


test_that("data_remove works with NSE", {
  expect_named(
    data_remove(iris, starts_with("Sepal")),
    c("Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, "Sepal"),
    c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, c("Sepal.Length", "Sepal.Width")),
    c("Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, regex("\\.")),
    "Species"
  )

  expect_named(
    data_remove(iris, Sepal.Width:Petal.Width),
    c("Sepal.Length", "Species")
  )

  expect_named(
    data_remove(iris, contains("Sep")),
    c("Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, contains("sep")),
    c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, contains("sep"), ignore_case = TRUE),
    c("Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, 1:3),
    c("Petal.Width", "Species")
  )

  expect_identical(
    colnames(data_remove(iris, c(1, 5))),
    colnames(iris)[2:4]
  )

  expect_identical(
    colnames(data_remove(iris, -1:-2)),
    colnames(iris)[1:2]
  )

  expect_identical(
    colnames(data_remove(iris, c(1, 4:5))),
    colnames(iris)[2:3]
  )

  expect_identical(
    colnames(data_remove(iris, "abc")),
    colnames(iris)
  )

  expect_named(
    data_remove(iris, "Species"),
    c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
  )

  expect_named(
    data_remove(iris, "species"),
    c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
  )

  expect_named(
    data_remove(iris, "species", ignore_case = TRUE),
    c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
  )
})


test_that("data_remove from other functions", {
  test_fun <- function(data, i) {
    data_remove(data, select = i)
  }
  expect_named(
    test_fun(iris, c("Sepal.Length", "Sepal.Width")),
    c("Petal.Length", "Petal.Width", "Species")
  )
})


# preserve attributes --------------------------

test_that("data_remove preserves attributes", {
  skip_if_not_installed("parameters")

  m <- lm(Sepal.Length ~ Species, data = iris)
  out <- parameters::parameters(m)
  a1 <- attributes(out)

  out2 <- data_remove(out, "SE")
  a2 <- attributes(out2)

  # attributes may not be in the same order
  expect_true(all(names(a1) %in% names(a2)))
  expect_identical(length(a1), length(a2))
})

# select helpers ------------------------------
test_that("data_remove regex", {
  expect_identical(
    names(data_remove(mtcars, select = "pg", regex = TRUE)),
    names(mtcars[-(1)])
  )
})