R haven:从导入的 SPSS 文件访问列标签
R haven: accessing column label from imported SPSS file
我在 SPSS 中有一个数据集,我正在使用 'haven' 库
读入 R
df <- structure(list(SC155Q09HA = structure(c(2, 1, 1, 2, 1, 2, 3,
4, 3, 1), label = "School's capacity using digital devices: An effective online learning support platform is available", labels = c(`Strongly disagree` = 1,
Disagree = 2, Agree = 3, `Strongly agree` = 4, `Valid Skip` = 5,
`Not Applicable` = 7, Invalid = 8, `No Response` = 9), class = "haven_labelled")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我正在尝试从数据框中提取标签,并且可以在 base R 中执行此操作:
library(tidyverse)
library(magrittr)
library(haven)
focus <- quo(SC156Q05HA)
attr(df$SC155Q09HA,"label")
>[1] "School's capacity using digital devices: An effective online learning support platform is available"
但不是以 dplyr 风格的方式使用变量进行选择:
df[quo_name(focus)] %>% attr("label")
>NULL
df %>% select(!!focus) %>% attr("label")
>NULL
我知道两个 none 工作示例 return tibbles,而第一个 return 是一个带标签的双打。我如何使它们相等?
你可以这样做:
focus <- quo(SC155Q09HA) # Changed to match the data provided
df %>% pull(!!focus) %>% attr("label")
[1] "School's capacity using digital devices: An effective online learning support platform is available"
您尝试使用 select()
将标题传递给 attr()
,它没有标签属性,因此它 returns NULL
.
如果您有多个标签要提取,请使用 purrr::map_chr()
df %>% purrr::map_chr(attr, "label")
我在 SPSS 中有一个数据集,我正在使用 'haven' 库
读入 Rdf <- structure(list(SC155Q09HA = structure(c(2, 1, 1, 2, 1, 2, 3,
4, 3, 1), label = "School's capacity using digital devices: An effective online learning support platform is available", labels = c(`Strongly disagree` = 1,
Disagree = 2, Agree = 3, `Strongly agree` = 4, `Valid Skip` = 5,
`Not Applicable` = 7, Invalid = 8, `No Response` = 9), class = "haven_labelled")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我正在尝试从数据框中提取标签,并且可以在 base R 中执行此操作:
library(tidyverse)
library(magrittr)
library(haven)
focus <- quo(SC156Q05HA)
attr(df$SC155Q09HA,"label")
>[1] "School's capacity using digital devices: An effective online learning support platform is available"
但不是以 dplyr 风格的方式使用变量进行选择:
df[quo_name(focus)] %>% attr("label")
>NULL
df %>% select(!!focus) %>% attr("label")
>NULL
我知道两个 none 工作示例 return tibbles,而第一个 return 是一个带标签的双打。我如何使它们相等?
你可以这样做:
focus <- quo(SC155Q09HA) # Changed to match the data provided
df %>% pull(!!focus) %>% attr("label")
[1] "School's capacity using digital devices: An effective online learning support platform is available"
您尝试使用 select()
将标题传递给 attr()
,它没有标签属性,因此它 returns NULL
.
如果您有多个标签要提取,请使用 purrr::map_chr()
df %>% purrr::map_chr(attr, "label")