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

Subsetting tibbles downgrades `labelled_spss` elements to `labelled` elements #360

Closed
huftis opened this Issue Mar 5, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@huftis
Copy link
Contributor

huftis commented Mar 5, 2018

I’m not sure this is a haven or a tibble bug, but the behaviour seems somewhat haven-specific, so I’m reporting it here. When subsetting a tibble containing a labelled_spss element, the element seems to be downgraded to just a labelled object.

More specifically, the (multi-)class 'labelled_spss', 'labelled' is changed to just 'labelled' and the na_values, na_range and label attributes are discarded (while the labels attribute is retained). Reprex:

library(haven)
library(tibble)
gen = labelled_spss(c(0, 0, 1, 99), c(Female = 0, Male = 1),
                    na_values = 99, na_range = c(990, 1000))
attr(gen, "label") = "Patient gender"

d = tibble(gen)
str(d$gen) # All metadata
#> Classes 'labelled_spss', 'labelled'  atomic [1:4] 0 0 1 99
#>   ..- attr(*, "labels")= Named num [1:2] 0 1
#>   .. ..- attr(*, "names")= chr [1:2] "Female" "Male"
#>   ..- attr(*, "na_values")= num 99
#>   ..- attr(*, "na_range")= num [1:2] 990 1000
#>   ..- attr(*, "label")= chr "Patient gender"
str(d[1:4,]$gen) # Only some metadata
#> Class 'labelled'  atomic [1:4] 0 0 1 99
#>   ..- attr(*, "labels")= Named num [1:2] 0 1
#>   .. ..- attr(*, "names")= chr [1:2] "Female" "Male"

(BTW, the reason I create the label attribute manually, is that the labelled_spss() (and labelled()) generator function don’t have a mechanism for creating it. However, the haven package does support the attribute. For example, reading SPSS files with read_spss() automatically creates the attribute if a variable has a label. And it’s a very useful attribute to have, so it shouldn’t be discarded.)

@huftis

This comment has been minimized.

Copy link
Contributor

huftis commented Mar 9, 2018

Having taken a closer look at this issue, I think it’s related to the [.labelled method (and the missing corresponding [.labelled_spss method).

@huftis

This comment has been minimized.

Copy link
Contributor

huftis commented Mar 9, 2018

Looks like this will be fixed by #353.

@lock

This comment has been minimized.

Copy link

lock bot commented Dec 17, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 17, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.