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

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 
主站蜘蛛池模板: 法库县| 阳曲县| 高淳县| 铜鼓县| 屏山县| 孝昌县| 墨竹工卡县| 松阳县| 铅山县| 闽清县| 克什克腾旗| 佛山市| 郁南县| 民权县| 民权县| 济南市| 磐石市| 卢氏县| 定州市| 什邡市| 饶平县| 新巴尔虎左旗| 庆云县| 乌拉特后旗| 扎赉特旗| 玛曲县| 洞口县| 盖州市| 澄城县| 确山县| 襄垣县| 湘乡市| 淮北市| 凤山市| 泰安市| 甘孜| 耒阳市| 西乌| 政和县| 眉山市| 会泽县|