Skip to content

Commit

Permalink
Fix issue introduced in former commit (#507)
Browse files Browse the repository at this point in the history
* Fix issue introduced in former commit

* add snapshot
  • Loading branch information
strengejacke authored May 27, 2024
1 parent 4331d97 commit 2c2a906
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
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.10.0.5
Version: 0.10.0.6
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
6 changes: 5 additions & 1 deletion R/data_to_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ data_to_long <- function(data,
# if columns in data frame have attributes (e.g. labelled data), `cbind()`
# won't work, so we need to remove them. We'll set them back later
not_stacked[] <- lapply(not_stacked, function(i) {
attributes(i) <- NULL
# we can't remove *all* attributes, this will convert factors into integers
attr(i, "label") <- NULL
attr(i, "labels") <- NULL
attr(i, "format.spss") <- NULL
class(i) <- setdiff(class(i), c("haven_labelled", "vctrs_vctr"))
i
})

Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/_snaps/data_to_long.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,46 @@
$ Item : chr "A1" "A2" "A3" "A4" ...
$ Score : int 2 4 3 4 4 2 3 3 4 4 ...

# don't convert factors to integer

Code
print(mtcars_long)
Output
cyl hp drat wt vs am gear carb am_f cyl_f id g value
1 4 93 3.85 2.320 1 1 4 1 1 4 3 mpg 22.80
2 4 93 3.85 2.320 1 1 4 1 1 4 3 qsec 18.61
3 4 93 3.85 2.320 1 1 4 1 1 4 3 disp 108.00
4 8 245 3.21 3.570 0 0 3 4 0 8 7 mpg 14.30
5 8 245 3.21 3.570 0 0 3 4 0 8 7 qsec 15.84
6 8 245 3.21 3.570 0 0 3 4 0 8 7 disp 360.00
7 4 66 4.08 2.200 1 1 4 1 1 4 10 mpg 32.40
8 4 66 4.08 2.200 1 1 4 1 1 4 10 qsec 19.47
9 4 66 4.08 2.200 1 1 4 1 1 4 10 disp 78.70
10 8 264 4.22 3.170 0 1 5 4 1 8 11 mpg 15.80
11 8 264 4.22 3.170 0 1 5 4 1 8 11 qsec 14.50
12 8 264 4.22 3.170 0 1 5 4 1 8 11 disp 351.00
13 6 110 3.08 3.215 1 0 3 1 0 6 4 mpg 21.40
14 6 110 3.08 3.215 1 0 3 1 0 6 4 qsec 19.44
15 6 110 3.08 3.215 1 0 3 1 0 6 4 disp 258.00
16 8 175 3.15 3.440 0 0 3 2 0 8 5 mpg 18.70
17 8 175 3.15 3.440 0 0 3 2 0 8 5 qsec 17.02
18 8 175 3.15 3.440 0 0 3 2 0 8 5 disp 360.00
19 8 335 3.54 3.570 0 1 5 8 1 8 12 mpg 15.00
20 8 335 3.54 3.570 0 1 5 8 1 8 12 qsec 14.60
21 8 335 3.54 3.570 0 1 5 8 1 8 12 disp 301.00
22 6 110 3.90 2.620 0 1 4 4 1 6 1 mpg 21.00
23 6 110 3.90 2.620 0 1 4 4 1 6 1 qsec 16.46
24 6 110 3.90 2.620 0 1 4 4 1 6 1 disp 160.00
25 6 110 3.90 2.875 0 1 4 4 1 6 2 mpg 21.00
26 6 110 3.90 2.875 0 1 4 4 1 6 2 qsec 17.02
27 6 110 3.90 2.875 0 1 4 4 1 6 2 disp 160.00
28 4 95 3.92 3.150 1 0 4 2 0 4 9 mpg 22.80
29 4 95 3.92 3.150 1 0 4 2 0 4 9 qsec 22.90
30 4 95 3.92 3.150 1 0 4 2 0 4 9 disp 140.80
31 4 62 3.69 3.190 1 0 4 2 0 4 8 mpg 24.40
32 4 62 3.69 3.190 1 0 4 2 0 4 8 qsec 20.00
33 4 62 3.69 3.190 1 0 4 2 0 4 8 disp 146.70
34 6 105 2.76 3.460 1 0 3 1 0 6 6 mpg 18.10
35 6 105 2.76 3.460 1 0 3 1 0 6 6 qsec 20.22
36 6 105 2.76 3.460 1 0 3 1 0 6 6 disp 225.00

14 changes: 14 additions & 0 deletions tests/testthat/test-data_to_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,17 @@ test_that("works with labelled data", {
expect_identical(nrow(out), 200L)
expect_identical(attributes(out$e42dep)$label, "elder's dependency")
})


test_that("don't convert factors to integer", {
data("mtcars")
mtcars <- mtcars[c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 18L, 29L, 31L), ]
mtcars$am_f <- factor(mtcars$am)
mtcars$cyl_f <- factor(mtcars$cyl)

mtcars$id <- factor(seq_len(nrow(mtcars)))
mtcars_long <- data_to_long(mtcars,
select = c("mpg", "qsec", "disp"), names_to = "g"
)
expect_snapshot(print(mtcars_long))
})

0 comments on commit 2c2a906

Please sign in to comment.