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
write_dta can't save labelled class without labels #442
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I think the issue arises in this defineVariable function:
https://github.com/tidyverse/haven/blob/7c24314b7a39f045d95719b21de553bbb2b7c865/src/DfWriter.cpp#L223
The function assumes the existence of a labelset if a variable is of class
haven_labelled. Later on in the function, the function is perfectly happy
to set labelset to a NULL value. Should be an easy fix: check and allow for
a NULL labelset.
…On Mon, Aug 26, 2019, 05:23 nbchan ***@***.***> wrote:
Wrote this function thanks to the workaround suggested by @Gootjes
<https://github.com/Gootjes>. Hope someone would find this useful.
fix_haven_labelled_write_problem <- function(df){
# Provides a workaround to the problem of writing haven-labelled variables (with null `labels`
# and non-null `label`) to Stata / SPSS files. Returns a dataframe compatible with functions
# such as `write_sav`.
# Reference: #442
# df: dataframe
# vector of problematic column names
cnames <- df %>%
select_if(~ is.labelled(.) & is.null(attributes(.)$labels)) %>%
colnames
# convert the problematic columns back to their generic data types
df_fixed <- df %>%
mutate_if(~ is.labelled(.) & is.null(attributes(.)$labels) & typeof(.) == 'character',
as.character) %>%
mutate_if(~ is.labelled(.) & is.null(attributes(.)$labels) & typeof(.) == 'integer',
as.integer) %>%
mutate_if(~ is.labelled(.) & is.null(attributes(.)$labels) & typeof(.) == 'double',
as.double)
# add an attribute called `label` to the respective columns
for(cname in cnames){
attributes(df_fixed[[cname]])$label <- df[[cname]] %>% attributes %$% label
}
return(df_fixed)
}
Created on 2019-08-26 by the reprex package <https://reprex.tidyverse.org>
(v0.3.0)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#442?email_source=notifications&email_token=AJAX3UZVDDI54FMMHPGXTMDQGNEEVA5CNFSM4G65GXU2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5DE2XI#issuecomment-524701021>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJAX3U22F7X4LASKGKY2ZEDQGNEEVANCNFSM4G65GXUQ>
.
|
Minimal reprex: library(haven)
x <- labelled(1:3, labels = NULL, label = 'x')
df <- data.frame(x = x)
write_dta(df, tempfile(fileext = '.dta'), version = 14)
#> Error in get_result(output = out, options) :
#> callr subprocess failed: could not start R, exited with non-zero status, has crashed or was killed |
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/ |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
According to the documentation of
labelled
, the argumentlabels
accepts aNULL
, which is reasonable, given that you might want to save a column with nolabels
but an actuallabel
.write_dta
throws a weird error when saving with thelabels
argument toNULL
.The text was updated successfully, but these errors were encountered: