简体   繁体   English

r expss 重新编码因子

[英]r expss recoding factor

I'd like to make small recoding on factor variable using examples from: https://cran.r-project.org/web/packages/expss/vignettes/tables-with-labels.html我想使用以下示例对因子变量进行小的重新编码: https://cran.r-project.org/web/packages/expss/vignettes/tables-with-labels.html

a<-c(1,2,1,3,5,4,1,3,2,2,1,1)
a<-factor(a,levels = c(1,2,3,4,5), labels = c("aa", "bb", "cc", "dd", "ee" ))

Let's assume I'd like to create new variable b, where "aa", "bb", "cc" would be now "xx", and rest would be copied.假设我想创建新变量 b,其中“aa”、“bb”、“cc”现在将是“xx”,并且 rest 将被复制。 It seems I can not refer to numbers in:似乎我无法引用以下数字:

b<-expss::recode(a,1:3~99)

As this return nothing.因为这没有回报。 So I tried to refer by label:所以我尝试通过 label 来引用:

b<-expss::recode(a, c("aa", "bb", "cc")~"xx", TRUE~copy, with_labels=FALSE)

But in this case new variable still has old levels stored:但在这种情况下,新变量仍然存储旧级别:

$levels
[1] "aa" "bb" "cc" "dd" "ee" "xx"

$class
[1] "factor"

So what should be the right approach to get new variable only with "xx", "dd", "ee" levels?那么仅使用“xx”、“dd”、“ee”级别获取新变量的正确方法应该是什么?

We can wrap with droplevels to drop those unused levels我们可以用droplevels包装来删除那些未使用的级别

b <- droplevels(expss::recode(a, c("aa", "bb", "cc")~"xx",
        TRUE~copy, with_labels=FALSE))
levels(b)
#[1] "dd" "ee" "xx"

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

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