简体   繁体   English

如何在R中将变量'labeled'的类更改为string或chr?

[英]How to change class of variable 'labelled' into string or chr in R?

How can i change a variable of class 'labelled' into a character variable only showing the labels as string or character? 如何将“标记”类的变量更改为仅将标签显示为字符串或字符的字符变量? So i only want to see the last attribute, see below for the structure of my variable. 所以我只想查看最后一个属性,请参阅下面的变量结构。

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" ...

I had the same question and couldn't find a designated function. 我有同样的问题,找不到指定的功能。

Converting to factor then character should work, though: 转换为因子然后字符应该工作,但是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM