我有 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/