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

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 
主站蜘蛛池模板: 民丰县| 若尔盖县| 昔阳县| 永新县| 聂拉木县| 巴彦淖尔市| 阜康市| 静海县| 抚顺市| 博湖县| 正镶白旗| 青浦区| 宁河县| 宁阳县| 文化| 武宣县| 乐至县| 安庆市| 正阳县| 香格里拉县| 卓尼县| 宣武区| 榆社县| 全椒县| 揭阳市| 获嘉县| 山西省| 秦皇岛市| 聂拉木县| 探索| 贵德县| 郑州市| 湖口县| 永修县| 临泽县| 石门县| 南京市| 宣武区| 平湖市| 江口县| 江西省|