读取R:中的SPS(.sav)文件时出现错误:文件不是任何受支持的SPSS格式

时间:2019-04-12 08:54:58

标签: r spss

尝试以R读取SPSS文件(.sav格式)会引发:

  

错误:文件不是任何受支持的SPSS格式。

在尝试使用foreignread.spss读取.sav文件时会发生这种情况。 尝试使用memsic软件包及其as.data.set(spss.system.file("my_file"))会引发:

  

spss.readheader(file)中的错误:不是sysfile

该文件是一个非常长的SPSS文件,包含超过200万个条目和数百种因素。这些因素各不相同:许多分类为“是” /“否” /“缺少” /“无”,一些为数字(IDS等),一些标记为文本(“状态一” /“状态2” /“状态3” )和一些混合在一起(“ 1” /“ 20” /“ 3732” /“技术问题”)。可悲的是,我不能给您我的部分数据(对隐私的严格限制,并且我没有SPSS许可证)。

读入此文件并将其存储为羽毛文件(.fea格式)已在另一台计算机上工作-可能已安装了另一个版本的R。我无法检查是哪个版本... 目前,我正在Windows 10的R版本3.4.4(2018-03-2015)中工作,并使用包memisc_0.99.17.2和foreign_0.8-71。该文件存储在服务器上,我的R安装在本地驱动器上的用户中。

这是我尝试过的代码:

require(foreign)
ws <- "my_workspace_in_local_user"
setwd(ws)
dataDir <- "my_directory_on_the_server_containing_the_file"
fn <- paste0(dataDir, "my_file.sav")
dat <- read.spss(fn, to.data.frame = TRUE)

require(foreign)
ws <- "my_workspace_in_local_user"
setwd(ws)
dataDir <- "my_directory_on_the_server_containing_the_file"
fn <- paste0(dataDir, "my_file.sav")
install.packages("memisc")
require("memisc")
dat <- as.data.set(fn, to.data.frame = TRUE)

有人知道为什么这行不通吗?我怀疑这是哪个版本的R和要使用的软件包的问题...?

1 个答案:

答案 0 :(得分:0)

您的第一组代码对我来说是在macOS 10.15.1(Catalina)和R 3.6.1上以及memisc_0.99.17.2和foreign_0.8-71。


R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[R.app GUI 1.70 (7684) x86_64-apple-darwin15.6.0]


> require(foreign)
Loading required package: foreign
> dataDir <- "~/Samples/English/"
> fn <- paste0(dataDir, "accidents.sav")
> dat <- read.spss(fn, to.data.frame = TRUE)
> print(dat)
    agecat gender accid    pop
1 Under 21 Female 57997 198522
2    21-25 Female 57113 203200
3    26-30 Female 54123 200744
4 Under 21   Male 63936 187791
5    21-25   Male 64835 195714
6    26-30   Male 66804 208239

“ accidents.sav”是随IBM SPSS Statistics版本19.0至26.0一起提供的示例数据文件。

如果该代码针对来自IBM SPSS的已知数据对您有用,则您可能会排除R版本和配置的原因。不幸的是,这可能意味着您的* .sav文件在某种程度上已损坏。