areg
######################################################
为了探讨从SPSS导入数据问题,我没有用现成的示例数据,特地从我的试验数据中复制一小部分制成he.sav,为了让大家看到内容,先把它转成.xls,再转成文本,如下:
######################################################
样地类型 样地序号 日期 物种 样点号 数目 盖度 平均高度
虎尾草高 101 "20,060,717" 虎尾草 1 17 80 28.33
虎尾草高 101 "20,060,717" 虎尾草 2 39 85 25.00
虎尾草高 101 "20,060,717" 虎尾草 3 98 80 23.67
虎尾草高 101 "20,060,717" 虎尾草 4 54 78 30.67
虎尾草高 101 "20,060,717" 虎尾草 5 6 50 19.00
芦苇1 103 "20,060,717" 芦苇 1 6 30 56.00
芦苇1 103 "20,060,717" 芦苇 2 8 34 64.67
芦苇1 103 "20,060,717" 芦苇 3 6 40 49.67
芦苇1 103 "20,060,717" 芦苇 4 10 70 35.00
芦苇1 103 "20,060,717" 芦苇 5 7 40 53.00
虎尾草高 104 "20,060,719" 虎尾草 1 57 100 25.67
虎尾草高 104 "20,060,719" 虎尾草 2 77 100 31.33
虎尾草高 104 "20,060,719" 虎尾草 3 12 70 23.67
虎尾草高 104 "20,060,719" 虎尾草 4 19 100 29.67
虎尾草高 104 "20,060,719" 虎尾草 5 26 100 29.33
芦苇2 105 "20,060,723" 芦苇 1 11 90 49.33
芦苇2 105 "20,060,723" 虎尾草 1 11 10 22.67
芦苇2 105 "20,060,723" 芦苇 2 4 40 53.33
芦苇2 105 "20,060,723" 虎尾草 2 10 8 38.33
芦苇2 105 "20,060,723" 芦苇 3 12 100 69.67
######################################################
######################################################
当变量名是中文时,R中导入不了,说明是不支持双字节码。当变量名改成拼音或字母时,能读。为便于与前面区别,我提前把he.sav改成heE.sav,我的当前目录设为D:/Rstudy 要导入的文件就放在这个文件夹中。
######################################################
> library(foreign)
# 利用“侧放效应”,把读入的文件赋一个对象,推荐用此种方式导入数据。
> heE<-read.spss("heE.sav",TRUE,TRUE) # 读入的是一个文本数据框,可用fix(heE)打开数据逻辑器
> heF<-read.spss("heE.sav",FALSE) # 读入的是一个文本,没有数据框的性质
> heG<-read.spss("heE.sav",TRUE) # 读入的是一个文本,没有数据框的性质
> heH<-read.spss("heE.sav") # 读入的是一个文本,没有数据框的性质
> heI<-read.spss("heE.sav", ,TRUE) # 读入的是一个文本数据框,可用fix(heE)打开数据逻辑器
# 即时反应
> read.spss("heE.sav",TRUE,TRUE)
> read.spss("heE.sav",FALSE)
> read.spss("heE.sav",TRUE)
> read.spss("heE.sav")
######################################################
下面是与这个函数相关的信息。
# 得到读取函数的介绍
read.spss(file, use.value.labels=TRUE, to.data.frame=FALSE,
max.value.labels=Inf, trim.factor.names=FALSE)
################################################################################
下面是read.spss的源代码
################################################################################
### Copyright 2000-2002 Saikat DebRoy <saikat$stat.wisc.edu>
### Douglas M. Bates <bates$stat.wisc.edu>,
### Thomas Lumley
### This file is part of the `foreign' package for R and related languages.
### It is made available under the terms of the GNU General Public
### License, version 2, or at your option, any later version,
### incorporated herein by reference.
###
### This program is distributed in the hope that it will be
### useful, but WITHOUT ANY WARRANTY; without even the implied
### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
### PURPOSE. See the GNU General Public License for more
### details.
###
### You should have received a copy of the GNU General Public
### License along with this program; if not, write to the Free
### Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
### Boston, MA 02110-1301 USA
read.spss <- function(file, use.value.labels = TRUE, to.data.frame = FALSE,
max.value.labels = Inf, trim.factor.names = FALSE)
{
trim <- function(strings) {
if (trim.factor.names)
gsub(" +$","",strings)
else
strings
}
rval <- .Call(do_read_SPSS, file)
vl <- attr(rval,"label.table")
has.vl <- which(!sapply(vl,is.null))
for(v in has.vl) {
nm <- names(vl)[[v]]
nvalues <- length(na.omit(unique(rval[[nm]])))
nlabels <- length(vl[[v]])
if (use.value.labels &&
(!is.finite(max.value.labels) || nvalues <= max.value.labels) &&
nlabels >= nvalues)
rval[[nm]] <- factor(rval[[nm]], levels = rev(vl[[v]]),
labels = rev(trim(names(vl[[v]]))))
else
attr(rval[[nm]],"value.labels") <- vl[[v]]
}
if (to.data.frame) {
varlab <- attr(rval, "variable.labels")
rval <- as.data.frame(rval)
attr(rval, "variable.labels") <- varlab
}
rval
}