1

I used read_dta to import Stata files into R. I am trying to get rid of attr(,"format.stata") which seems to have been stored into a list.

I tried different things based on my google search but none of them have worked.

> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
> df2$peer_id_spec <- zap_labels(zap_formats(df2$peer_id_spec))
> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
> df2$peer_id_spec <- zap_formats(df2$peer_id_spec)
> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
> df2$peer_id_spec <- zap_formats(zap_labels(df2$peer_id_spec))
> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
> df2$peer_id_spec <- zap_labels(zap_formats(df2$peer_id_spec))
> df2$peer_id_spec[[1]]
[1] 41
attr(,"format.stata")
[1] "%9.0g"
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
PaulaSpinola
  • 531
  • 2
  • 10
  • Have you tried [zap_formats(df2)](https://haven.tidyverse.org/reference/zap_formats.html) ? – neilfws Aug 29 '22 at 11:58
  • 1
    Also worth noting that these are _attributes_ that describe your data frame - not the data itself - so unless they are causing you some issue, you can leave them alone. – neilfws Aug 29 '22 at 12:08

1 Answers1

2

Just delete them.

dta <- haven::read_dta(path)

attributes(dta$sepallength)  ## has format.stata attribute
# $label
# [1] "Sepal.Length"
# 
# $format.stata
# [1] "%9.0g"

dta <- lapply(dta, `attr<-`, 'format.stata', NULL)  ## delete attributes
attributes(dta$sepallength)  ## cleaned
# $label
# [1] "Sepal.Length"

Alternatively use readstata13::read.dta13.

dta <- readstata13::read.dta13(path)
attributes(dta$sepallength)  ## no attributes
# NULL

Data:

path <- system.file("examples", "iris.dta", package = "haven")
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • thank you! how can I do this for every column in the dataframe/data.table? – PaulaSpinola Aug 29 '22 at 12:23
  • @PaulaSpinola I'm not quite sure about your data since it's not [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), it looks nested somehow. But try `df2$peer_id_spec[[1]] <- lapply(df2$peer_id_spec[[1]], `attr<-`, 'format.stata', NULL)`. – jay.sf Aug 29 '22 at 12:26