Skip to content

Commit 5d92576

Browse files
authored
support haven_labelled-class (#468)
* support haven_labelled * fix * fix
1 parent b65df4d commit 5d92576

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 0.9.0.2
4+
Version: 0.9.0.3
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ S3method(to_factor,Date)
167167
S3method(to_factor,character)
168168
S3method(to_factor,data.frame)
169169
S3method(to_factor,default)
170+
S3method(to_factor,double)
170171
S3method(to_factor,factor)
172+
S3method(to_factor,haven_labelled)
171173
S3method(to_factor,logical)
172174
S3method(to_factor,numeric)
173175
S3method(to_numeric,Date)
@@ -179,6 +181,7 @@ S3method(to_numeric,data.frame)
179181
S3method(to_numeric,default)
180182
S3method(to_numeric,double)
181183
S3method(to_numeric,factor)
184+
S3method(to_numeric,haven_labelled)
182185
S3method(to_numeric,logical)
183186
S3method(to_numeric,numeric)
184187
S3method(unnormalize,data.frame)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGES
55
* `rescale()` gains `multiply` and `add` arguments, to expand ranges by a given
66
factor or value.
77

8+
* `to_factor()` and `to_numeric()` now support class `haven_labelled`.
9+
810
# datawizard 0.9.0
911

1012
NEW FUNCTIONS

R/to_factor.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ to_factor.character <- to_factor.numeric
7979
#' @export
8080
to_factor.Date <- to_factor.numeric
8181

82+
#' @export
83+
to_factor.haven_labelled <- to_factor.numeric
84+
85+
#' @export
86+
to_factor.double <- to_factor.numeric
87+
8288
#' @rdname to_factor
8389
#' @export
8490
to_factor.data.frame <- function(x,

R/to_numeric.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ to_numeric.double <- to_numeric.numeric
157157
#' @export
158158
to_numeric.logical <- to_numeric.numeric
159159

160+
#' @export
161+
to_numeric.haven_labelled <- to_numeric.numeric
162+
160163
#' @export
161164
to_numeric.Date <- function(x, verbose = TRUE, ...) {
162165
if (verbose) {

tests/testthat/test-data_to_factor.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,24 @@ test_that("data_read, convert many labels correctly", {
141141
expect_snapshot(data_tabulate(to_factor(d$c12c)))
142142
unlink(temp_file)
143143
})
144+
145+
146+
test_that("to_factor works with haven_labelled, convert many labels correctly", {
147+
skip_if_not_installed("withr")
148+
withr::with_tempfile("temp_file", fileext = ".sav", code = {
149+
request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav")
150+
httr::stop_for_status(request)
151+
writeBin(httr::content(request, type = "raw"), temp_file)
152+
153+
d <- haven::read_spss(temp_file)
154+
x <- to_factor(d$c172code)
155+
expect_identical(
156+
levels(x),
157+
c(
158+
"low level of education",
159+
"intermediate level of education",
160+
"high level of education"
161+
)
162+
)
163+
})
164+
})

tests/testthat/test-data_to_numeric.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,23 @@ test_that("to_numeric regex", {
160160
to_numeric(mtcars, select = "mpg")
161161
)
162162
})
163+
164+
165+
test_that("to_numeric works with haven_labelled, convert many labels correctly", {
166+
skip_on_cran()
167+
skip_if_not_installed("httr")
168+
skip_if_not_installed("haven")
169+
skip_if_not_installed("withr")
170+
skip_if_not_installed("curl")
171+
skip_if_offline()
172+
173+
withr::with_tempfile("temp_file", fileext = ".sav", code = {
174+
request <- httr::GET("https://raw.github.com/easystats/circus/main/data/EFC.sav")
175+
httr::stop_for_status(request)
176+
writeBin(httr::content(request, type = "raw"), temp_file)
177+
178+
d <- haven::read_spss(temp_file)
179+
x <- to_numeric(d$c172code)
180+
expect_identical(as.vector(table(x)), c(180L, 506L, 156L))
181+
})
182+
})

0 commit comments

Comments
 (0)