Skip to content

write_dta can't save numeric labelled variables without value labels #606

@domingoscisci

Description

@domingoscisci

Hi all,
I have a data.frame of double (no integer) variables that I want to label (only variable label, not value labels). If I try to use the labelled() function, a vector of class dbl+lbl is correctly created, but it cannot be saved in a Stata file using write_dta(). If I use the attr() function the problem does not arise.
It is probably not correct to use labelled() to label the variable only. In this case, perhaps it might be useful to make this explicit within the documentation (maybe I missed something?).

Here is a reprex:

library(tibble)
library(haven)


dat <- tibble(
  a = c(0.1, 0.2, 0.3, 0.4, 0.5)
)

# label variable with haven::labelled
dat$a <- labelled(dat$a, label = "test label")

# write_dta fails
write_dta(dat, tempfile(fileext = "dta"))
#> Error: Stata only supports labelling with integers.
#> Problems: `a`


dat <- tibble(
  a = c(0.1, 0.2, 0.3, 0.4, 0.5)
)

# label variable with attr
attr(dat$a, "label") <- "test label"

# write_dta ok
write_dta(dat, tempfile(fileext = "dta"))

Thank you for your help,
Domingo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions