7.forcats处理因子

一、创建因子

library(tidyverse)
library(forcats)
x1 <- c("Dec", "Apr", "Jan", "Mar") 
sort(x1)  # 这样创建的因子没有排序的意义

month_levels <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
y1 <- factor(x1, levels = month_levels)
y1
sort(y1)  # 这样创建的因子可以按照设定的要求排序

z1 <- c("Dec", "Api", "Jan", "Mar") 
y2 <- factor(z1, levels = month_levels)
y2  # 这样创建的因子如果不在有效集合内都自动转换为NA

y2 <- parse_factor(z1, levels = month_levels)  # 利用readr::parse_factor()可以显示错误信息

二、查看因子水平

forcats::gss_cat
gss_cat %>%count(race)

三、修改因子水平

gss_cat %>% count(partyid)

gss_cat %>% 
  mutate(partyid = fct_recode(partyid,
                              "Republican, strong"= "Strong republican",
                              "Republican, weak"= "Not str republican")) %>%  # fct_recode()修改因子名称
  count(partyid)

gss_cat %>%
  mutate(partyid = fct_recode(partyid,
                              "other" = "No answer",
                              "other" = "Don't know",
                              "other" = "Other party")) %>%  # 将多个因子赋予一个新因子,达到合并效果
  count(partyid)

gss_cat %>%
  mutate(partyid = fct_collapse(partyid,
                                other = c("No answer", "Don't know", "Other party"),
                                rep = c("Strong republican", "Not str republican"),
                                ind = c("Ind,near rep", "Independent", "Ind,near dem")
                                )) %>%  # 用fct_collapse()合并多个水平
  count(partyid)

你可能感兴趣的:(7.forcats处理因子)