[Edit: The responses are great; thank you. Unfortunately, I oversimplified my reprex. My actual data have dashes inside the parentheses sometimes. Also, as was noted, sometimes there are more than two variants. I am editing my reprex accordingly.]
I am tidying a table of prices for products from a given manufacturer. The models come in variants, with each variant of the same model being the same price, so the original table creator listed the price only once, and distinguished the models with an internal parenthesized alternative scheme. E.g. "1000-a" and "1000-b" are variants of the same model, indicated on the price list as "1000-a(b)". I would like to list each model out on its own row.
Reprex:
prices <- data.frame(
model = c("1000-a(-b)", "2000-a(b)", "150-L(R)", "TX-a(-b)-J", "350-LX(-S)(-AB)"),
price = seq(1999,5999,1000)
)
> prices
model price
1 1000-a(-b) 1999
2 2000-a(b) 2999
3 150-L(R) 3999
4 TX-a(-b)-J 4999
5 350-LX(-S)(-AB) 5999
Sometimes the differences are single characters, sometimes multiple. Sometimes they are at the end of the model number, sometimes in the middle. I would like code which produces this:
> desired_prices
model price
1 1000-a 1999
2 1000-b 1999
3 2000-a 2999
4 2000-b 2999
5 150-L 3999
6 150-R 3999
7 TX-a-J 4999
8 TX-b-J 4999
9 350-LX 5999
10 350-S 5999
11 350-AB 5999
This seems related to the question Split comma-separated strings in a column into separate rows, but in my case the values aren't comma delimited.
How may I produce this split/expansion? A tidyverse solution is preferred but not required.