【问题标题】:working with factors: reordering, relabeling and recoding in R使用因素:在 R 中重新排序、重新标记和重新编码
【发布时间】:2020-01-11 17:30:05
【问题描述】:

我在“分解”字符变量和标记/重新编码不同级别时遇到了一些麻烦。使用tidyverseBase Rrecodefct_collapse 等是否有有效的方法(即更少的编码)...谢谢

#This is what I have (a character variable)
x <- c("No", "Yes", "No2", "No3", "Maybe", "undecided", 
       "probably", "dont know", NA)
x
#I want a factor with three ordered levels as follows:

#where No = c("No", "No2", "No3")
#Yes = c("Yes")
#other = c("Maybe", "undecided", "probably")
#NA = c("dont know", NA)
# and the levels would be 0 = "No",  1 = "Yes" and 2 = "Maybe"
#that is:
#xfact
# [1] No    Yes   other
# Levels: No Yes other
#
# as.integer(xfact)
# [1] 0, 1, 2```

【问题讨论】:

    标签: r tidyverse recode forcats


    【解决方案1】:

    应该这样做:

    library(tidyverse)
    
    x <- c("No", "Yes", "No2", "No3", "Maybe", "undecided", 
           "probably", "dont know", NA)
    
    na_if(x, "dont know") %>% 
      fct_collapse(
        no = c("No", "No2", "No3"),
        yes = c("Yes"),
        other = c("Maybe", "undecided", "probably")
      ) %>% 
      fct_inorder()
    #> [1] no    yes   no    no    other other other <NA>  <NA> 
    #> Levels: no yes other
    

    reprex package (v0.3.0) 于 2020 年 1 月 13 日创建

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多