r - 将 SPSS 读入 R 时 attr(*, "value.labels") 是什么?

标签 r import spss

我有 SPSS 文件,但没有 SPSS。所以我想在 R 中打开它。

如果我使用以下方式打开它:

library(foreign)
dat <- read.spss("file.sav", to.data.frame=TRUE)

我收到警告

re-encoding from CP1252
Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

如果我理解正确,编码通知不是问题(我使用的是 UTF-8 语言环境),但是有关级别的警告是什么意思?

如果我使用以下方式打开文件:

dat <- read.spss("file.sav", to.data.frame=TRUE, use.value.labels = FALSE)

警告消失了,但我不确定我所做的是否正确。

此外,调用 str(dat) 会给出如下输出:

pt_art  : atomic  1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "value.labels")= Named chr  "2" "1"
  .. ..- attr(*, "names")= chr  "IPT" "VT"

attr(*, "value.labels") 是什么意思?我知道“pt_art”的意思是“心理治疗类型”,“IPT”和“VT”是两种治疗类型,“2”和“1”是代表这些类型的数字代码,所以我们所拥有的是级别和R 中的标签,但如何正确地将其转移到 R 中?

最佳答案

当您尝试使用包含重复值的 labels 参数定义因子时,会出现警告。

(x <- sample(letters[1:4], 10, replace = TRUE))
##  [1] "b" "c" "d" "d" "b" "c" "d" "c" "c" "c"
factor(x, levels = x)
##  [1] b c d d b c d c c c
## Levels: b c d d b c d c c c
## Warning message:
## In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
##   duplicated levels will not be allowed in factors anymore

SPSS 通常使用值标签来表示分类变量(应成为 R 中的因子)。但请注意 ?read.spss 帮助页面中的此部分。

Occasionally in SPSS, value labels will be added to some values of a continuous variable (e.g. to distinguish different types of missing data), and you will not want these variables converted to factors. By setting 'max.value.labels' you can specify that variables with a large number of distinct values are not converted to factors even if they have value labels. In addition, variables will not be converted to factors if there are non-missing values that have no value label. The value labels are then returned in the '"value.labels"' attribute of the variable.

关于r - 将 SPSS 读入 R 时 attr(*, "value.labels") 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21228518/

相关文章:

javascript - Web-Worker 导入的脚本能否依次导入另一个脚本?

sql - 将 Visual Foxpro 数据库导入到 SQL Server

python - 如何将变量的值传递给python中的import语句

statistics - 线性回归与SPSS中的普通最小二乘一样吗?

r - 对于每个大组,选择 R 中可用的最佳子组

r - 在ggplot上绘制shapefile

r - Tidyverse 命令已弃用 : T test inside a summarise , 然后报告所有结果

r - 如何在 RStudio 的 ioslides 中为每张幻灯片添加 Logo

python - 使用 GetValueChar 找不到变量

statistics - 统计余弦分析,