如何将变量“labelled”的类更改为R中的字符串或字符?

Ben*_*amp 4 label functional-programming r char

如何将“labelled”类的变量更改为字符变量,仅将标签显示为字符串或字符?所以我只想查看最后一个属性,请参阅下面的变量结构。

Class 'labelled'  atomic [1:918] 4 12 13 20 26 36 40 1 4 13 ...
..- attr(*, "format.spss")= chr "F8.0"
..- attr(*, "labels")= Named num [1:40] 1 2 3 4 5 6 7 8 9 10 ...
.. ..- attr(*, "names")= chr [1:40] "People management" "HR" "Self management" "Email" ...
Run Code Online (Sandbox Code Playgroud)

shi*_*iro 5

没有指定的函数,但转换为因子然后再次转换为字符可以工作:

s2 <- labelled(c(1, 1, 2), c(Male = 1, Female = 2)) # example from haven:labelled
as.character(as_factor(s2)) # use haven::as_factor
Run Code Online (Sandbox Code Playgroud)