读写 Stata SPSS 和 SAS 文件
包 foreign
和 haven
可用于从各种其他统计软件包(如 Stata,SPSS 和 SAS 以及相关软件)导入和导出文件。每个支持的数据类型都有一个 read
函数来导入文件。
# loading the packages
library(foreign)
library(haven)
library(readstata13)
library(Hmisc)
最常见数据类型的一些示例:
# reading Stata files with `foreign`
read.dta("path\to\your\data")
# reading Stata files with `haven`
read_dta("path\to\your\data")
foreign
包可以读取 stata(.dta)
文件中的 Stata 7-12 版本。根据开发页面,read.dta
或多或少地被冻结,并且在 13+版本中不会更新。对于更新版本的 Stata,你可以使用 readstata13
软件包或 haven
。对于 readstata13
,文件是
# reading recent Stata (13+) files with `readstata13`
read.dta13("path\to\your\data")
用于读取 SPSS 和 SAS 文件
# reading SPSS files with `foreign`
read.spss("path\to\your\data.sav", to.data.frame = TRUE)
# reading SPSS files with `haven`
read_spss("path\to\your\data.sav")
read_sav("path\to\your\data.sav")
read_por("path\to\your\data.por")
# reading SAS files with `foreign`
read.ssd("path\to\your\data")
# reading SAS files with `haven`
read_sas("path\to\your\data")
# reading native SAS files with `Hmisc`
sas.get("path\to\your\data") #requires access to saslib
# Reading SA XPORT format ( *.XPT ) files
sasxport.get("path\to\your\data.xpt") # does not require access to SAS executable
SAScii
包提供了接受 SAS SET 导入代码并构造可以使用 read.fwf
处理的文本文件的功能。事实证明,它对于导入大型公共发布数据集非常可靠。支持位于 https://github.com/ajdamico/SAScii
要将数据帧导出到其他统计包,可以使用写入函数 write.foreign()
。这将写入 2 个文件,一个包含数据,另一个包含另一个包读取数据所需的指令。
# writing to Stata, SPSS or SAS files with `foreign`
write.foreign(dataframe, datafile, codefile,
package = c("SPSS", "Stata", "SAS"), ...)
write.foreign(dataframe, "path\to\data\file", "path\to\instruction\file", package = "Stata")
# writing to Stata files with `foreign`
write.dta(dataframe, "file", version = 7L,
convert.dates = TRUE, tz = "GMT",
convert.factors = c("labels", "string", "numeric", "codes"))
# writing to Stata files with `haven`
write_dta(dataframe, "path\to\your\data")
# writing to Stata files with `readstata13`
save.dta13(dataframe, file, data.label = NULL, time.stamp = TRUE,
convert.factors = TRUE, convert.dates = TRUE, tz = "GMT",
add.rownames = FALSE, compress = FALSE, version = 117,
convert.underscore = FALSE)
# writing to SPSS files with `haven`
write_sav(dataframe, "path\to\your\data")
也可以通过这种方式使用 read.spss
读取 SPSS 存储的文件:
foreign::read.spss('data.sav', to.data.frame=TRUE, use.value.labels=FALSE,
use.missings=TRUE, reencode='UTF-8')
# to.data.frame if TRUE: return a data frame
# use.value.labels if TRUE: convert variables with value labels into R factors with those levels
# use.missings if TRUE: information on user-defined missing values will used to set the corresponding values to NA.
# reencode character strings will be re-encoded to the current locale. The default, NA, means to do so in a UTF-8 locale, only.