0

I'm trying to reorder a string in a dataframe for all of the values in a column.

I have values such as:

F123/1K
F234/2Q
F678/8W

and I want it to look like:

K1231
Q2342
W6788

Is there a way to change all the strings at once rather than the crude way of doing each individually in a loop?

Any assistance would be greatly appreciated.

MaskedMonkey
  • 107
  • 1
  • 6

1 Answers1

2

We can use sub to capture patterns as a group and then do the rearrangement of the backreference of the captured groups in the replacement

sub("^.(.{3}).(.)(.)", "\\3\\1\\2", df1$col)
#[1] "K1231" "Q2342" "W6788"

data

df1 <- structure(list(col = c("F123/1K", "F234/2Q", "F678/8W")), 
   .Names = "col", class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662