Collapse some categorical variables in tidyverse

Viewed 471

I'm working with a large dataset that has several locations. However, for one of my analyses, two locations "Wells1" and "Wells2", need to be collapsed into a single location "Wells". All other locations should keep their current names.

There are several excellent questions showing how to do this using different basic R functions (#1, #2), but I was wondering if anyone knows which tidyverse function would achieve the same goal.

The only thing I've come up with so far is:

case_when(recvDeployName %in% c("Wells1", "Wells2") ~ "Wells") 

However, I get the following error message: Error: Case 1 (.) must be a two-sided formula, not a list

I suspect, I need to specify what should be done with the other categories, but I'm not sure what that is.

1 Answers

The case_when can be written as

case_when(recvDeployName %in% c("Wells1", "Wells2") ~ "Wells",
                TRUE ~ recvDeployName)
Related