Reputation: 151
I have done some recoding to my dataframe to reflect groups that I wish to run analyses on, but need to code everything else as "Other". See below:
DF:
Var1
BIO
CHEM
Arts
BIO
Zoo
Edu
Math
BIO
What I want is:
DF: Var1 BIO CHEM Other BIO Other Other Other BIO PSY
I have tried this code:
DF[ Var1 ==c(!"BIO", "CHEM", "PSY")] <- "Other"
But this does not work. Any help is appreciated. Thanks!
Upvotes: 0
Views: 35
Reputation: 3671
Here is an adapted version of your code. It works for character strings.
DF$Var1[!DF$Var1 %in% c("BIO", "CHEM", "PSY")] <- "Other"
For factors you may use:
fct_other(DF$Var1, keep = c("BIO", "CHEM", "PSY"), other_level = "Other")
Upvotes: 2