版本与运行环境信息

  1. Date:2021-5-18
  2. R version 4.0.3 (2020-10-10)
  3. Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. Running under: Windows 10 x64 (build 18363)

载入相关包

  1. library("tidyverse")

forcats包函数的使用

测试数据为:gss_cat

as_factor()

用法:as_factor(x, ...)

将给定的数据转化为因子,适用于字符串、逻辑值、数字…..

x: 需要转换为因子的序列

  1. nu_seq <- c(1:5) %>% as_factor()
  2. nu_seq <- c(1:5) %>% as.factor()
  3. str_seq <- c("T_1","T_2","T_3") %>% as_factor()
  4. log <- c(T,T,T,F,F,T,F) %>% as_factor()
  5. ###
  6. > str_seq
  7. [1] T_1 T_2 T_3
  8. Levels: T_1 T_2 T_3
  9. > log
  10. [1] TRUE TRUE TRUE FALSE FALSE TRUE FALSE
  11. Levels: FALSE TRUE

fct_anon

fct_anon(f, prefix = "")

将给定的因子的相应的index转换为因子,通过prefix =可以指定因子的开头字符

  1. f <- as_factor(c("b", "b", "a", "c", "c", "c"))
  2. > f %>% fct_anon()
  3. [1] 2 2 1 3 3 3
  4. Levels: 1 2 3
  5. > f %>% fct_anon(prefix = "ST_")
  6. [1] ST_3 ST_3 ST_1 ST_2 ST_2 ST_2
  7. Levels: ST_1 ST_2 ST_3
  8. #可以指定多个前缀,依次会按照顺序添加
  9. > f %>% fct_anon(prefix = c("ST1_","ST2_","ST3_"))
  10. [1] ST3_3 ST3_3 ST2_2 ST1_1 ST1_1 ST1_1
  11. Levels: ST1_1 ST2_2 ST3_3