使用“ as_factor”转换“ haven_labelled”后,标签丢失

时间:2019-05-31 16:15:34

标签: r tidyverse r-haven

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"))。

0 个答案:

没有答案