WFU-TLC / flc_discussion_board

A repository for discussing questions and issues in the Data Analysis with R (FLC)
https://wfu-tlc.github.io/
0 stars 0 forks source link

Simpler syntax for 'forcats' code to simplify coding groups #14

Closed adanieljohnson closed 5 years ago

adanieljohnson commented 5 years ago

My dataframe has two columns, Subject and Structure, that list the hand-coded classifications I want to evaluate. The block of code below uses fct_recode (from forcats) to reduce the original list of 4 possible Subject factors down to 2: ingroup (formerly 3_technical) and outgroup (formerly 1_basic, 2_writing, or 4_logic).

comments_raw_binarycoded <- comments_raw %>% 
  mutate(subject = fct_recode(subject, ingroup = "3_technical", outgroup="1_basic", outgroup="2_writing", outgroup="4_logic"))

This works, but I had to write in each option individually. Doing multiple comments for a larger number of factors is going to be tedious, because I will need to rewrite this line for each ingroup/outgroup pairing.

Guide to fct_recode makes it look like I should be able to recode all NON-ingroup items with one command, using something like this pseudo-code, but I cannot get it to run:

comments_raw_binarycoded <- comments_raw %>%
  mutate(subject = fct_recode(subject, ingroup = "3_technical", outgroup= NOT "3_technical"))

Am I missing something obvious in syntax?

Related question: would it be simpler to create two lists for the items to be recoded, then pass the list as a variable?

medewitt commented 5 years ago

@adanieljohnson check this out:

https://forcats.tidyverse.org/reference/fct_collapse.html

I think that the example that they use is exactly what yo are trying to do

partyid2 <- fct_collapse(gss_cat$partyid,
  missing = c("No answer", "Don't know"),
  other = "Other party",
  rep = c("Strong republican", "Not str republican"),
  ind = c("Ind,near rep", "Independent", "Ind,near dem"),
  dem = c("Not str democrat", "Strong democrat")
)

Might be easier...I have never tried the NOT though...