3

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.

tnt
  • 1,149
  • 14
  • 24

1 Answers1

1

The case_when can be written as

case_when(recvDeployName %in% c("Wells1", "Wells2") ~ "Wells",
                TRUE ~ recvDeployName)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • That makes a lot of sense, but I'm still getting the same error message. Can this be used with several other functions? Like in here: tagRecvSummary_tree2 <- df.alltags_tree.sub %>% case_when(recvDeployName %in% c("Wells1", "Wells2") ~ "Wells", TRUE ~ recvDeployName) #%>% group_by(mfgID) %>% summarize(Indi=length(unique(mfgID)), Locations=length(unique(recvDeployName))) – tnt Oct 24 '18 at 18:45
  • @user3220999 What is `df.alltags_tree.sub`? Is it a `data.frame`, then you can use `case_when` within `mutate` i.e. `df.alltags_tree.sub %>% mutate(recvDeployName = case_when(recvDeployName %in% c("Wells1", "Wells2") ~ "Wells", TRUE ~ recvDeployName))` – akrun Oct 24 '18 at 18:46
  • 1
    Yes, it is a dataframe. Thanks! That worked perfectly! – tnt Oct 24 '18 at 18:50