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.

7
  • 3
    gsub("([0-9]+)-([0-9]+)", "\\2-\\1", "12-4567") Commented Mar 27, 2023 at 19:17
  • You can try gsub("(\\w+)-(\\w+)", "\\2-\\1", c("XX-XXXX", "12-4567")) Commented Mar 27, 2023 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? Commented Mar 27, 2023 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! Commented Mar 27, 2023 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. Commented Mar 27, 2023 at 19:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.