Closed adanieljohnson closed 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...
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).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: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?