首页 > 解决方案 > 在 R 中读取 SPS (.sav) 文件时出错:文件不是任何受支持的 SPSS 格式

问题描述

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

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

尝试使用foreign和读取 .sav 文件时会发生这种情况read.spss。尝试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 和软件包的问题......?

标签: rspss

解决方案


您的第一组代码在 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 文件以某种方式损坏。


推荐阅读