r - 使用haven读取.sav(SPSS_文件): Change labelled vector to character string or factor

标签 r spss r-haven

我正在使用 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/

相关文章:

python - 自动导出 IBM SPSS Data Collection 调查?

python - SPSS 22 中的 Numpy

r - 使用 r 从 s3 读取 zip 文件

r - dplyr bind_rows 不保留变量标签

r - 使用 haven::labelled 动态创建值标签

r - HTML(或 R Shiny)音频缓存

r - data.table中Join和Not Join的组合?

r - 在R中获取每个月的第一天和最后一天

regex - 删除字符串中句点前的所有字符

merge - SPSS - 合并具有 ID 变量重复案例和新案例/变量的文件