Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support haven_labelled-class #468

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.9.0.2
Version: 0.9.0.3
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ S3method(to_factor,Date)
S3method(to_factor,character)
S3method(to_factor,data.frame)
S3method(to_factor,default)
S3method(to_factor,double)
S3method(to_factor,factor)
S3method(to_factor,haven_labelled)
S3method(to_factor,logical)
S3method(to_factor,numeric)
S3method(to_numeric,Date)
Expand All @@ -179,6 +181,7 @@ S3method(to_numeric,data.frame)
S3method(to_numeric,default)
S3method(to_numeric,double)
S3method(to_numeric,factor)
S3method(to_numeric,haven_labelled)
S3method(to_numeric,logical)
S3method(to_numeric,numeric)
S3method(unnormalize,data.frame)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGES
* `rescale()` gains `multiply` and `add` arguments, to expand ranges by a given
factor or value.

* `to_factor()` and `to_numeric()` now support class `haven_labelled`.

# datawizard 0.9.0

NEW FUNCTIONS
Expand Down
6 changes: 6 additions & 0 deletions R/to_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ to_factor.character <- to_factor.numeric
#' @export
to_factor.Date <- to_factor.numeric

#' @export
to_factor.haven_labelled <- to_factor.numeric

#' @export
to_factor.double <- to_factor.numeric

#' @rdname to_factor
#' @export
to_factor.data.frame <- function(x,
Expand Down
3 changes: 3 additions & 0 deletions R/to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ to_numeric.double <- to_numeric.numeric
#' @export
to_numeric.logical <- to_numeric.numeric

#' @export
to_numeric.haven_labelled <- to_numeric.numeric

#' @export
to_numeric.Date <- function(x, verbose = TRUE, ...) {
if (verbose) {
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-data_to_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,24 @@ test_that("data_read, convert many labels correctly", {
expect_snapshot(data_tabulate(to_factor(d$c12c)))
unlink(temp_file)
})


test_that("to_factor works with haven_labelled, convert many labels correctly", {
skip_if_not_installed("withr")
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_factor(d$c172code)
expect_identical(
levels(x),
c(
"low level of education",
"intermediate level of education",
"high level of education"
)
)
})
})
20 changes: 20 additions & 0 deletions tests/testthat/test-data_to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,23 @@ test_that("to_numeric regex", {
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))
})
})
Loading