I have several strings in this format. The separator is a dash ( -
) and each "thing" in between is a marker.
string <- "FA-I2-I2-I2-EX-I2-I3-FA-I1-I2-TR-I1-I2-FA-I3-I1-FAFANR-I3-I2-TR-I1-I2-I1-I2-FA-I2-I1-I3-FAQU-I1-I2-I2-I2-NR-I2-I2-NR-I1-I2-I1-NR-I3-QU-I2-I3-QUNR-I2-I1-NRQUQU-I2-I1-EX"
I want to identify cases wherever markers containing the letter "I" occurs in a row (ie the markers I1, I2, and I3). Then I want to replace those with a description that has no separators. For example, the very beginning of the string should be converted as follows:
FA-I2I2I2-EX
So basically all I want to do is to remove all the dashes between markers containing "I".
Here's a somewhat convoluted solution:
string1 <- gsub(string, pattern = "I1", replacement = "ZI1Z")
string2 <- gsub(string1, pattern = "I2", replacement = "ZI2Z")
string3 <- gsub(string2, pattern = "I3", replacement = "ZI3Z")
string4 <- gsub(string3, pattern = "Z-Z", replacement = "")
string5 <- gsub(string4, pattern = "Z", replacement = "")
which gives:
"FA-I2I2I2-EX-I2I3-FA-I1I2-TR-I1I2-FA-I3I1-FAFANR-I3I2-TR-I1I2I1I2-FA-I2I1I3-FAQU-I1I2I2I2-NR-I2I2-NR-I1I2I1-NR-I3-QU-I2I3-QUNR-I2I1-NRQUQU-I2I1-EX"
Is there a more elegant way of accomplishing this?
So basically all I want to do is to remove all the dashes between markers containing "I".
You can use lookaround assertions if your case is as simple as it sounds.
gsub('(?<=I\\d)-(?=I\\d)', '', string, perl = TRUE)
# [1] "FA-I2I2I2-EX-I2I3-FA-I1I2-TR-I1I2-FA-I3I1-FAFANR-I3I2-TR-I1I2I1I2-FA-I2I1I3-FAQU-I1I2I2I2-NR-I2I2-NR-I1I2I1-NR-I3-QU-I2I3-QUNR-I2I1-NRQUQU-I2I1-EX"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.