I want to create an additional column in a dataframe, which is a substring from an existing column in the dataframe, but using different start and end points for each row.
Specifically, the column "codes" in the example below contains a single colon character ":" somewhere in the string. This location varies in each string. I want to take the two characters before and two characters after the colon, as well as the colon.
An example of what I currently have:
letters <- c("A", "B", "C")
codes <- c("lksjdfi99:99lksjdf", "nsj78:12osjsm", "a12:67opaidsf")
df <- data.frame(letters, codes)
print(df)
letters codes
1 A lksjdfi99:99lksjdf
2 B nsj78:12osjsm
3 C a12:67opaidsf
This is an example of what I would like to have:
letters codes new_col
1 A lksjdfi99:99lksjdf 99:99
2 B nsj78:12osjsm 78:12
3 C a12:67opaidsf 12:67
Any help would be appreciated.