r - 在 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 和软件包的问题......?
解决方案
您的第一组代码在 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 文件以某种方式损坏。
推荐阅读
- c# - 如何在 c# httpClient 中重置设置?
- php - PHP 注意:未定义的偏移量:0 可修复?
- python - 我通过在列表和数组中附加项目来计算 numpy 数组和普通列表的时间
- jenkins - 詹金斯和 MSBuild 错误
- java - 二维数组嵌套的列行而不是行列
- java - 如何在 viewsource 模式下使用 openStream?
- c++ - 当我单击正在运行的程序之外的任何位置时,Qt 所有的图纸都会消失
- javascript - 在 CSV 文件中搜索值并给出输出
- hyperledger-fabric - Hyperledger Fabric 网络 - 通道和分类帐
- r - 修改时复制;运行此代码时会发生什么?x <- 列表(1:10);x[[2]] <- x