
上QQ阅读APP看书,第一时间看更新
2.4 读取SPSS、SAS、STATA数据
在做统计分析的时候,往往可能还会涉及其他统计软件,如SAS、SPSS。不同软件储存数据的格式不一样,这时需要一个工具对不同格式的数据进行操作。在R语言中可以使用haven包,这个包用于读取其他统计软件的数据,如SAS的sas7bdat格式数据、SPSS的sav格式数据。
1.读取SAS数据:read_sas( )
使用system.file来获取haven包中的iris.sas7bdat数据集的路径,然后通过read_sas( )函数来读取sas格式的数据:

2.读取SPSS数据: read_sav( )
使用system.file来获取haven包中的SPSS格式的数据集(iris.sav数据集),然后使用read_sav( )函数读取数据:

3.读取STATA数据:read_dta( )
先获取STATA格式的数据集(iris.dta数据集),然后使用read_dta( )读取数据:

另外,将R语言环境中的数据转变为SAS、SPSS、STATA的数据,可以通过如下函数实现。
(1)write_sas( ):write_sas( )的第一个参数是源数据集,第二个参数是数据保存的路径。

(2)write_sav( ):write_sav( )的第一个参数是源数据集,第二个参数是数据保存的路径。

(3)write_dta( ):write_dta( )的第一个参数是源数据集,第二个参数是数据保存的路径和文件名。
