11.5 Modifying factor values
gss_cat %>% count(partyid)
## # A tibble: 10 x 2
## partyid n
## <fct> <int>
## 1 No answer 154
## 2 Don't know 1
## 3 Other party 393
## 4 Strong republican 2314
## 5 Not str republican 3032
## 6 Ind,near rep 1791
## 7 Independent 4119
## 8 Ind,near dem 2499
## 9 Not str democrat 3690
## 10 Strong democrat 3490
forcats::fct_recode(gss_cat$partyid,
"Rep, Strong" = "Strong republican",
"Dem, Strong" = "Strong democrat") %>%
head(30)
## [1] Ind,near rep Not str republican Independent
## [4] Ind,near rep Not str democrat Dem, Strong
## [7] Not str republican Ind,near dem Not str democrat
## [10] Rep, Strong Not str democrat Ind,near rep
## [13] Dem, Strong Rep, Strong Ind,near dem
## [16] Dem, Strong Rep, Strong Independent
## [19] Not str democrat Independent Ind,near dem
## [22] Rep, Strong Independent Ind,near rep
## [25] Not str democrat Dem, Strong Not str democrat
## [28] Rep, Strong Dem, Strong Independent
## 10 Levels: No answer Don't know Other party ... Dem, Strong
gss_cat_recoded <- gss_cat %>%
mutate(party_id_recode = fct_recode(
partyid,
"Rep, Strong" = "Strong republican",
"Dem, Strong" = "Strong democrat"))
11.5.1 Double check your work
https://gist.github.com/jennybc/04b71bfaaf0f88d9d2eb
# do a cross tab in R
table(gss_cat_recoded$partyid, gss_cat_recoded$party_id_recode, useNA = 'always')
##
## No answer Don't know Other party Rep, Strong
## No answer 154 0 0 0
## Don't know 0 1 0 0
## Other party 0 0 393 0
## Strong republican 0 0 0 2314
## Not str republican 0 0 0 0
## Ind,near rep 0 0 0 0
## Independent 0 0 0 0
## Ind,near dem 0 0 0 0
## Not str democrat 0 0 0 0
## Strong democrat 0 0 0 0
## <NA> 0 0 0 0
##
## Not str republican Ind,near rep Independent
## No answer 0 0 0
## Don't know 0 0 0
## Other party 0 0 0
## Strong republican 0 0 0
## Not str republican 3032 0 0
## Ind,near rep 0 1791 0
## Independent 0 0 4119
## Ind,near dem 0 0 0
## Not str democrat 0 0 0
## Strong democrat 0 0 0
## <NA> 0 0 0
##
## Ind,near dem Not str democrat Dem, Strong <NA>
## No answer 0 0 0 0
## Don't know 0 0 0 0
## Other party 0 0 0 0
## Strong republican 0 0 0 0
## Not str republican 0 0 0 0
## Ind,near rep 0 0 0 0
## Independent 0 0 0 0
## Ind,near dem 2499 0 0 0
## Not str democrat 0 3690 0 0
## Strong democrat 0 0 3490 0
## <NA> 0 0 0 0