【问题标题】:Error when reading SPS (.sav) file in R: file not in any supported SPSS format在 R 中读取 SPS (.sav) 文件时出错:文件不是任何受支持的 SPSS 格式
【发布时间】:2019-04-12 08:54:57
【问题描述】:

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

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

当尝试使用 foreignread.spss 读取 .sav 文件时会发生这种情况。 尝试memsicpackage 和它的as.data.set(spss.system.file("my_file")) raises:

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 和软件包的问题...?

【问题讨论】:

  • 您是否能够读取任何其他 .sav 文件?您是否能够读取与当前文件位于同一目录中的任何其他 .sav 文件?您是否检查过 fn 是否真的包含 .sav 文件的正确路径?也许它只是一个小错字或什么?
  • 你可以先下载文件试试。
  • @TinglTanglBob 我刚刚尝试从 [a spss 教程] (spss.allenandunwin.com.s3-website-ap-southeast-2.amazonaws.com/…) 打开“调查”.sav 文件。我将它存储在与我的文件相同的目录中并且它有效。 fn 确实包含指向该文件的链接,我三重检查了没有错字。
  • @RomanLuštrik 这样文件就存放在工作目录中了吗?我已将文件存储在工作目录中;仍然得到同样的错误。
  • 直接从 .sav 获取文件对您来说重要吗?如果没有,我会尝试用 spss 打开 .sav 文件并将其另存为 .txt。之后,我会尝试使用任何标准函数(如 read.table 或 data.table::fread)用 r 读取 .txt。

标签: r spss


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多