简体   繁体   中英

Import dta into R: get rid of Stata formatting

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM