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

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 
主站蜘蛛池模板: 丰顺县| 靖远县| 长岭县| 余庆县| 仲巴县| 基隆市| 漠河县| 贡山| 江都市| 华安县| 灵璧县| 乐安县| 胶州市| 雅安市| 沅江市| 屯门区| 永登县| 八宿县| 祁连县| 米易县| 镇江市| 夏河县| 隆德县| 彝良县| 山东省| 治多县| 红安县| 永清县| 德保县| 磴口县| 米脂县| 保定市| 成武县| 台安县| 南川市| 历史| 沂源县| 军事| 原平市| 宁德市| 定结县|