我正在使用 haven
图书馆阅读.sav
(SPSS) 文件导入 R。
一些值被读取为 labelled
vector
.
这是一个例子:
> str(df$instructional_practice)
Class 'labelled' atomic [1:4136] 2 2 6 6 8 8 NaN NaN 17 1 ...
..- attr(*, "label")= chr "intructional practice teacher is using when signaled"
..- attr(*, "format.spss")= chr "F8.2"
..- attr(*, "labels")= Named num [1:18] 1 2 3 4 5 6 7 8 9 10 ...
.. ..- attr(*, "names")= chr [1:18] "1 Lecture" "2 Seatwk-Ind" "3 Review-Ind" "4 Seatwk-Grp" ...
我如何获得
vector
的值是标签名称?
最佳答案
您可以使用 haven::as_factor
将标记向量转换为因子,使用标签作为级别。
您可以在单个向量上使用它:
df$instructional_practice = as_factor(df$instructional_practice)
但是您也可以在整个 data.frame 上使用它。默认使用
as_factor
在 data.frame 上将所有标签转换为任何标记变量的因子水平。df = as_factor(df)
关于r - 使用haven读取.sav(SPSS_文件): Change labelled vector to character string or factor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306808/