当将haven_labelled
变量转换为因子变量时,我(似乎)失去了潜在的“标签”(使用tidyverse
我认为的术语......)。
# this sets up a factor var x with non-continuous numeric values
library(tidyverse)
library(labelled)
x <- sample( c(1, 5, 10, 20), 1000000, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) )
x_tib <- as_tibble(x) %>%
set_value_labels(value = c("Letter A" = 1,
"Letter B" = 5,
"Letter C" = 10,
"Letter D" = 20))
的属性x_tib$value
如我所料
attributes(x_tib$value)
glimpse(x_tib$value)
> attributes(x_tib$value)
$labels
Letter A Letter B Letter C Letter D
1 5 10 20
$class
[1] "haven_labelled"
> glimpse(x_tib$value)
'haven_labelled' num [1:1000000] 10 10 10 5 10 5 10 10 10 10 ...
- attr(*, "labels")= Named num [1:4] 1 5 10 20
..- attr(*, "names")= chr [1:4] "Letter A" "Letter B" "Letter C" "Letter D"
但是,在我将其转换为因子变量后(如haven
文档中所建议的那样),我似乎失去了原来的“标签”(1、5、10、20 变为 1、2、3、4)。
attributes(as_factor(x_tib$value))
glimpse(as_factor(x_tib$value))
> attributes(as_factor(x_tib$value))
$levels
[1] "Letter A" "Letter B" "Letter C" "Letter D"
$class
[1] "factor"
> glimpse(as_factor(x_tib$value))
Factor w/ 4 levels "Letter A","Letter B",..: 3 3 3 2 3 2 3 3 3 3 ...
我可以保留底层的“标签”吗?
注意 - 我知道我可以在as_factor
(例如as_factor(x_tib$value, "value")
或as_factor(x_tib$value, "both")
)的“级别”选项中对它们进行编码。