File: test-ranktransform.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 (142 lines) | stat: -rw-r--r-- 3,447 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
131
132
133
134
135
136
137
138
139
140
141
142
test_that("ranktransform works with NAs", {
  x <- c(NA_real_, NA_real_)
  expect_identical(ranktransform(x), x)
})

test_that("ranktransform works with factors", {
  x <- factor(c("apple", "bear", "banana", "dear"))
  expect_identical(ranktransform(x), x)
})

test_that("ranktransform works with unique value vectors", {
  x <- c(1L, 1L, 1L)

  expect_identical(suppressWarnings(ranktransform(x)), x)

  expect_warning(
    ranktransform(x),
    "Variable `x` contains only one unique value and will not be normalized."
  )
})

test_that("ranktransform works with two unique value vectors", {
  x <- c(1L, 1L, 1L, 2L, 2L, 2L)

  expect_identical(suppressWarnings(ranktransform(x)), c(2, 2, 2, 5, 5, 5))

  expect_warning(
    ranktransform(x),
    "Consider converting it"
  )
})

test_that("signed rank works as expected", {
  x <- c(-1, 2, -3, 4)

  sr <- ranktransform(x, sign = TRUE)
  r <- ranktransform(x, sign = FALSE)

  expect_identical(sr, x) # unchanged
  expect_identical(r, c(2, 3, 1, 4))

  x <- c(1, -2, -2, 4, 0, 3, -14, 0)
  expect_warning(ranktransform(x, sign = TRUE))
  expect_true(all(is.na(suppressWarnings(
    ranktransform(x, sign = TRUE)[c(5, 8)]
  ))))
})

test_that("argument 'zeros' works", {
  x <- c(-1, 0, 2, -3, 4)
  expect_warning(
    ranktransform(x, sign = TRUE),
    "cannot be sign-rank"
  )
  expect_identical(
    ranktransform(x, sign = TRUE, zeros = "signrank"),
    c(-2, 0, 3, -4, 5)
  )
  expect_error(
    ranktransform(x, sign = TRUE, zeros = "foo"),
    "should be one of"
  )
})

test_that("ranktransform works with data frames", {
  set.seed(123)
  expect_snapshot(ranktransform(BOD))
})


# with grouped data -------------------------------------------

test_that("ranktransform works with data frames (grouped data)", {
  skip_if_not_installed("poorman")

  set.seed(123)
  value1 <- sample.int(20, 9, replace = TRUE)
  set.seed(456)
  value2 <- sample.int(20, 9, replace = TRUE)

  test_df <- data.frame(
    id = rep(c("A", "B", "C"), each = 3),
    value1 = value1,
    value2 = value2,
    stringsAsFactors = FALSE
  )

  # nolint start: nested_pipe_linter
  expect_identical(
    test_df %>%
      poorman::group_by(id) %>%
      ranktransform(exclude = "id") %>%
      poorman::ungroup(),
    data.frame(
      id = rep(c("A", "B", "C"), each = 3),
      value1 = c(2, 3, 1, 1, 2, 3, 2, 1, 3),
      value2 = c(3, 2, 1, 1, 3, 2, 2, 3, 1),
      stringsAsFactors = FALSE
    )
  )
  # nolint end
})


test_that("ranktransform works with data frames containing NAs (grouped data)", {
  skip_if_not_installed("poorman")

  set.seed(789)
  value1 <- sample(c(1:15, NA), 9, replace = TRUE)
  set.seed(10)
  value2 <- sample(c(1:15, NA), 9, replace = TRUE)

  test_df <- data.frame(
    id = rep(c("A", "B", "C"), each = 3),
    value1 = value1,
    value2 = value2,
    stringsAsFactors = FALSE
  )

  # nolint start: nested_pipe_linter
  expect_identical(
    test_df %>%
      poorman::group_by(id) %>%
      ranktransform(exclude = "id") %>%
      poorman::ungroup(),
    data.frame(
      id = rep(c("A", "B", "C"), each = 3),
      value1 = c(2, NA, 1, 1, 3, 2, 2, NA, 1),
      value2 = c(3, 1, 2, NA, 2, 1, 3, 1, 2),
      stringsAsFactors = FALSE
    )
  )
  # nolint end
})

# select helpers ------------------------------
test_that("ranktransform regex", {
  expect_identical(
    ranktransform(mtcars, select = "pg", regex = TRUE),
    ranktransform(mtcars, select = "mpg")
  )
})