Home > Back-end >  Import dta into R: get rid of Stata formatting
Import dta into R: get rid of Stata formatting

Time:08-30

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"

CodePudding user response:

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")
  • Related