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

fix data_write for new haven #437

Merged
merged 6 commits into from
Jun 20, 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.8.0
Version: 0.8.0.1
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# datawizard (devel)

BUG FIXES

* Fixed issues in `data_write()` when writing labelled data into SPSS format
and vectors were of different type as value labels.

# datawizard 0.8.0

BREAKING CHANGES
Expand Down
18 changes: 15 additions & 3 deletions R/data_write.r
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,30 @@ data_write <- function(data,
insight::format_alert("Preparing data file: converting variable types.")
}
x[] <- lapply(x, function(i) {
# make sure we have labelled class for labelled data
value_labels <- attr(i, "labels", exact = TRUE)
variable_label <- attr(i, "label", exact = TRUE)
# factor requires special preparation to save levels as labels
# haven:::vec_cast_named requires "x" and "labels" to be of same type
if (is.factor(i)) {
# factor requires special preparation to save levels as labels
haven::labelled(
x = as.numeric(i),
labels = stats::setNames(seq_along(levels(i)), levels(i)),
label = variable_label
)
} else if (!is.null(value_labels) || !is.null(variable_label)) {
# make sure we have labelled class for labelled data
haven::labelled(x = i, labels = value_labels, label = variable_label)
# character requires special preparation to save value labels
# haven:::vec_cast_named requires "x" and "labels" to be of same type
if (is.character(i)) {
haven::labelled(
x = i,
labels = stats::setNames(as.character(value_labels), names(value_labels)),
label = variable_label
)
} else {
# this should work for the remaining types...
haven::labelled(x = i, labels = value_labels, label = variable_label)
}
} else {
# non labelled data can be saved "as is"
i
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-data_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ test_that("data_write, SPSS", {
})


tmp <- tempfile(fileext = ".sav")
on.exit(unlink(tmp))

test_that("data_write, SPSS, mixed types of labelled vectors", {
d <- data.frame(
a = 1:3,
b = letters[1:3],
c = factor(letters[1:3]),
d = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01")),
e = c(TRUE, FALSE, FALSE),
stringsAsFactors = FALSE
)

# Date and Logical cannot be labelled
d$a <- assign_labels(d$a, variable = "First", values = c("one", "two", "three"))
d$b <- assign_labels(d$b, variable = "Second", values = c("A", "B", "C"))
d$c <- assign_labels(d$c, variable = "Third", values = c("ey", "bee", "see"))

# expect message, but no error
expect_message(data_write(d, "test.sav"), regex = "Preparing")
})



# Stata -------------------------------------

Expand Down