0

I have a dataset which consists of a bunch of codes. For the most part, the data is entered in this format.

a <- XX-XXXX
b <- 12-4567

The X is any number between 0 to 9. It should be noted that the number of digits AFTER the dash can vary. For the purposes of this example, there are four numbers the dash, but the data can contain 1 number or 9 numbers after the dash.

However, some data has been entered as this format:

c <- XXXX-XX
d <- 4567-12

What I would like to do is to flip it so that whatever comes after the dash is put back into the beginning and the rest of the number is separated by a dash (-). The actual order of the digits does not need to change.

I've tried str_replace_all, but that just seems to remove the whole string after the dash.

Any thoughts on how to resolve this issue is greatly appreciated. I'm using R.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • 3
    `gsub("([0-9]+)-([0-9]+)", "\\2-\\1", "12-4567")` – r2evans Mar 27 '23 at 19:17
  • You can try `gsub("(\\w+)-(\\w+)", "\\2-\\1", c("XX-XXXX", "12-4567"))` – ThomasIsCoding Mar 27 '23 at 19:19
  • @r2evans Thank you for the solution. I quickly copied the formula and it worked. If I'm interpreting this correctly, I assume this part captures the formatting of my data. But what does "\\2-\\1" mean? – user13893942 Mar 27 '23 at 19:30
  • @ThomasIsCoding - thank you for your solution for as well. I'm not very well very versed in R, can you explain what this segment "(\\w+)-(\\w+)" means? Thank you! – user13893942 Mar 27 '23 at 19:31
  • @akrun Unfortunately, in my data, there are situations where there are more than 4 digits so your solution, while it works seems to be too specific. Sorry if I wasn't clear on that parameter in my original post. – user13893942 Mar 27 '23 at 19:31
  • @user13893942 `\\w+` matches multiple word characters – ThomasIsCoding Mar 27 '23 at 19:33
  • The `\\1` and `\\2` are back-references to the `(...)` grouping in the first pattern. See https://stackoverflow.com/a/22944075/3358272 for good notes about regular expressions. – r2evans Mar 27 '23 at 20:23

0 Answers0