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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
test_that("convert data frame to numeric", {
expect_snapshot(to_numeric(head(ToothGrowth), dummy_factors = TRUE))
expect_snapshot(to_numeric(head(ToothGrowth), dummy_factors = FALSE))
})
test_that("convert character to numeric", {
expect_identical(to_numeric(c("xyz", "ab")), c(2, 1))
})
test_that("convert character to numeric Date", {
expect_warning(expect_identical(to_numeric(as.Date("2022-01-01")), as.numeric(as.Date("2022-01-01"))))
expect_warning(expect_identical(to_numeric(as.POSIXct("2022-01-01")), as.numeric(as.POSIXct("2022-01-01"))))
expect_warning(expect_identical(to_numeric(as.POSIXlt("2022-01-01")), as.numeric(as.POSIXlt("2022-01-01"))))
})
test_that("convert character to numeric preserve levels", {
x <- head(as.factor(mtcars$gear))
expect_identical(
to_numeric(x, dummy_factors = FALSE),
c(2, 2, 2, 1, 1, 1)
)
expect_identical(
to_numeric(x, dummy_factors = FALSE, preserve_levels = TRUE),
c(4, 4, 4, 3, 3, 3)
)
})
test_that("convert character to numeric lowest", {
d <- head(mtcars)
d$vs <- as.factor(d$vs)
model <- glm(vs ~ wt + mpg, data = d, family = "binomial")
expect_identical(
to_numeric(insight::get_response(model), dummy_factors = FALSE),
c(1, 1, 2, 2, 1, 2)
)
expect_identical(
to_numeric(insight::get_response(model), dummy_factors = FALSE, lowest = 0),
c(0, 0, 1, 1, 0, 1)
)
})
test_that("convert factor to numeric", {
f <- factor(substring("statistics", 1:10, 1:10))
expect_snapshot(to_numeric(f, dummy_factors = TRUE))
})
test_that("convert factor to numeric", {
expect_identical(to_numeric(c("abc", "xyz")), c(1, 2))
expect_identical(to_numeric(c("123", "789")), c(123, 789))
expect_identical(to_numeric(c("1L", "2e-3")), c(1, 0.002))
expect_identical(to_numeric(c("1L", "2e-3", "ABC")), c(1, 2, 3))
})
test_that("convert factor to numeric, dummy factors", {
expect_identical(
to_numeric(c("abc", "xyz"), dummy_factors = TRUE),
data.frame(abc = c(1, 0), xyz = c(0, 1)),
ignore_attr = TRUE
)
expect_identical(
to_numeric(c("1L", "2e-3", "ABC"), dummy_factors = TRUE),
data.frame(`1L` = c(1, 0, 0), `2e-3` = c(0, 1, 0), ABC = c(0, 0, 1)),
ignore_attr = TRUE
)
})
test_that("convert factor to numeric, append", {
data(efc)
expect_identical(
colnames(to_numeric(efc, dummy_factors = TRUE)),
c("c12hour", "e16sex", "e42dep.1", "e42dep.2", "e42dep.3", "e42dep.4", "c172code", "neg_c_7"),
ignore_attr = TRUE
)
expect_identical(
colnames(to_numeric(efc, dummy_factors = TRUE, append = TRUE)),
c(
"c12hour", "e16sex", "e42dep", "c172code", "neg_c_7", "e42dep_n",
"e42dep_n.1", "e42dep_n.2", "e42dep_n.3", "e42dep_n.4"
),
ignore_attr = TRUE
)
expect_identical(
colnames(to_numeric(efc, append = TRUE, dummy_factors = FALSE)),
c("c12hour", "e16sex", "e42dep", "c172code", "neg_c_7", "e42dep_n"),
ignore_attr = TRUE
)
expect_identical(
colnames(to_numeric(efc, append = FALSE, dummy_factors = FALSE)),
c("c12hour", "e16sex", "e42dep", "c172code", "neg_c_7"),
ignore_attr = TRUE
)
})
test_that("convert factor to numeric, all numeric", {
data(mtcars)
expect_identical(to_numeric(mtcars), mtcars)
})
test_that("convert factor to numeric, dummy factors, with NA", {
x1 <- factor(rep(c("a", "b"), 3))
x2 <- factor(c("a", NA_character_, "a", "b", "a", "b"))
x3 <- factor(c(NA_character_, "b", "a", "b", "a", "b"))
x4 <- factor(c("a", "b", "a", "b", "a", NA_character_))
x5 <- factor(c(NA_character_, "b", "a", "b", "a", NA_character_))
x6 <- factor(c(NA_character_, "b", NA_character_, "b", "a", NA_character_))
x7 <- factor(c(
NA_character_, "b", "a", "b", "a", "b", NA_character_, "b",
"a", NA_character_, "a", "b", "a", "b", "a", NA_character_
))
# same observations are missing
expect_identical(
which(!complete.cases(to_numeric(x1, dummy_factors = TRUE))),
which(is.na(x1))
)
expect_identical(
which(!complete.cases(to_numeric(x2, dummy_factors = TRUE))),
which(is.na(x2))
)
expect_identical(
which(!complete.cases(to_numeric(x3, dummy_factors = TRUE))),
which(is.na(x3))
)
expect_identical(
which(!complete.cases(to_numeric(x4, dummy_factors = TRUE))),
which(is.na(x4))
)
expect_identical(
which(!complete.cases(to_numeric(x5, dummy_factors = TRUE))),
which(is.na(x5))
)
expect_identical(
which(!complete.cases(to_numeric(x6, dummy_factors = TRUE))),
which(is.na(x6))
)
expect_identical(
which(!complete.cases(to_numeric(x7, dummy_factors = TRUE))),
which(is.na(x7))
)
# output has same number of observation as input
expect_identical(nrow(to_numeric(x1, dummy_factors = TRUE)), length(x1))
expect_identical(nrow(to_numeric(x2, dummy_factors = TRUE)), length(x2))
expect_identical(nrow(to_numeric(x3, dummy_factors = TRUE)), length(x3))
expect_identical(nrow(to_numeric(x4, dummy_factors = TRUE)), length(x4))
expect_identical(nrow(to_numeric(x5, dummy_factors = TRUE)), length(x5))
expect_identical(nrow(to_numeric(x6, dummy_factors = TRUE)), length(x6))
expect_identical(nrow(to_numeric(x7, dummy_factors = TRUE)), length(x7))
})
test_that("to_numeric, inverse factor levels", {
f <- c(0, 0, 1, 1, 1, 0)
x1 <- factor(f, levels = c(0, 1))
x2 <- factor(f, levels = c(1, 0))
out <- to_numeric(x1, dummy_factors = FALSE, preserve_levels = FALSE)
expect_identical(out, c(1, 1, 2, 2, 2, 1))
out <- to_numeric(x2, dummy_factors = FALSE, preserve_levels = FALSE)
expect_identical(out, c(2, 2, 1, 1, 1, 2))
out <- to_numeric(x1, dummy_factors = FALSE, preserve_levels = TRUE)
expect_identical(out, c(0, 0, 1, 1, 1, 0))
out <- to_numeric(x2, dummy_factors = FALSE, preserve_levels = TRUE)
expect_identical(out, c(1, 1, 0, 0, 0, 1))
})
# select helpers ------------------------------
test_that("to_numeric regex", {
expect_identical(
to_numeric(mtcars, select = "pg", regex = TRUE),
to_numeric(mtcars, select = "mpg")
)
})
test_that("to_numeric works with haven_labelled, convert many labels correctly", {
skip_on_cran()
skip_if_not_installed("httr")
skip_if_not_installed("haven")
skip_if_not_installed("withr")
skip_if_not_installed("curl")
skip_if_offline()
withr::with_tempfile("temp_file", fileext = ".sav", code = {
request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav")
httr::stop_for_status(request)
writeBin(httr::content(request, type = "raw"), temp_file)
d <- haven::read_spss(temp_file)
x <- to_numeric(d$c172code)
expect_identical(as.vector(table(x)), c(180L, 506L, 156L))
})
})
test_that("to_numeric preserves correct label order", {
x <- factor(c(1, 2, 3, 4))
x <- assign_labels(x, values = c("one", "two", "three", "four"))
out <- to_numeric(x, dummy_factors = FALSE)
expect_identical(
attributes(out)$labels,
c(one = 1, two = 2, three = 3, four = 4)
)
# correctly reverse scale
out <- to_numeric(reverse_scale(x), dummy_factors = FALSE)
expect_identical(
attributes(out)$labels,
c(one = 4, two = 3, three = 2, four = 1)
)
# factor with alphabetical values
x <- factor(letters[1:4])
x <- assign_labels(x, values = c("one", "two", "three", "four"))
out <- to_numeric(x, dummy_factors = FALSE)
expect_identical(
attributes(out)$labels,
c(one = 1, two = 2, three = 3, four = 4)
)
# correctly reverse scale
out <- to_numeric(reverse_scale(x), dummy_factors = FALSE)
expect_identical(
attributes(out)$labels,
c(one = 4, two = 3, three = 2, four = 1)
)
})
|