I have a strings that look like this:
problem <- c("GROUP 1", "GROUP 1 & GROUP 2", "GROUP 1 & GROUP 2 & GROUP 3", "GROUP 1 & GROUP 2 & GROUP 3 & GROUP 4")
In between each group, there's " & ". I want to use R (either sub()
or something from the stringr
package) to replace every " &" with a "," when there's more than one "&" present. However, I don't want the final "&" to be changed. How would I do that so it looks like:
#Note: Only the 3rd and 4th strings should be changed
solution <- c("GROUP 1", "GROUP 1 & GROUP 2", "GROUP 1, GROUP 2 & GROUP 3", "GROUP 1, GROUP 2, GROUP 3 & GROUP 4")
In the actual string, there could be an infinite number of "&"s, so I don't want to hard code a limit if possible.