官术网_书友最值得收藏!

Introduction to the haven and foreign R packages

The R package called haven is for import and export from SPSS, Stata and SAS files. The package is for Labelled Data Utility Functions, which is a collection of many small functions dealing with labelled data, such as reading and writing data between R and other statistical software packages such as SAS, SPSS, or Stata, and working with labelled data.

This includes easy ways to get, set, and change value and variable label attributes, convert labelled vectors into factors or numeric values (and vice versa), and deal with multiple declared missing values. The following example is about writing several specific outputs:

library(haven)
x<-1:100
y<-matrix(x,50,2)
z<-data.frame(y)
colnames(z)<-c("a","b")
write_sas(z,"c:/temp/tt.sas7bdat")
write_spss(z,"c:/temp/tt.sav")
write_stata(z,"c:/temp/tt.dta")

Another R package called foreign can be used to read SPSS, and SAS data. Here is one example. First, let's download a SPSS dataset called airline_passanges.sav at http://calcnet.mth.cmich.edu/org/spss/Prj_airlinePassengers.htm. Assume that the related SPCC data is saved under c:/temp:

library(foreign) 
x<-read.spss("c:/temp/airline_passengers.sav", to.data.frame=TRUE) 
dim(x) 
[1] 144   1 
主站蜘蛛池模板: 洪江市| 射洪县| 霍城县| 新安县| 山西省| 墨竹工卡县| 乌鲁木齐市| 林口县| 哈尔滨市| 秦皇岛市| 樟树市| 洮南市| 米林县| 招远市| 麟游县| 绥化市| 自治县| 家居| 金溪县| 新源县| 承德县| 龙游县| 延津县| 巴东县| 红河县| 瑞金市| 丹江口市| 九寨沟县| 恭城| 临夏县| 都安| 广东省| 宿松县| 安达市| 海盐县| 庄河市| 青龙| 扎囊县| 汶川县| 大渡口区| 远安县|