Reputation: 8404
I have a character below:
ms<-"DC, MD, MA, Wash, Balt, Bos, x, y, z"
and I wish to reorder it like this:
ms<-"DC, Wash, MD, Balt, MA, Bos, x, y, z"
Since I want to repeat this many times for different characters but with the same logic I do not want to change it manually every time but with a general function. Supposing that its order is now 1,2,3,4,5,6,7,8,9 I want to convert it to 1,4,2,5,3,6,7,8,9. The character may have from 6 to any number of substances but I want the convertion to take place only to the first 6, the rest should stay untouchable. I tried something like ms <- ms[1,4,2,5,3,6]
but with no result.
Upvotes: 0
Views: 71
Reputation: 887028
If we have a custom order, then split the string, order and then paste
the contents together
toString(strsplit(ms, ",\\s*")[[1]][c(1, 4, 2, 5, 3, 6)])
#[1] "DC, Wash, MD, Balt, MA, Bos"
It can also be done if the number of words in the string is less i.e. by capturing as a group and then rearranging the backreference in the replacement with gsub
. This becomes cumbersome once there are more words
gsub("^(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+)$",
"\\1, \\4, \\2, \\5, \\3, \\6", ms)
With the new example in OP's post, it is not clear what is general from the index pattern
v1 <- strsplit(ms, ",\\s*")[[1]]
toString(v1[c(1, 4, 2, 5, 3, 6, 7:length(v1))])
#[1] "DC, Wash, MD, Balt, MA, Bos, x, y, z"
If only the first 6 words to be rearranged, then instead of splitting up the whole string, it can be arranged within gsub
gsub("^(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+),\\s+(\\w+)(.*)$",
"\\1, \\4, \\2, \\5, \\3, \\6\\7", ms)
#[1] "DC, Wash, MD, Balt, MA, Bos, x, y, z"
Here, we capture the words within braces ((\\w+)
) and the backreference looks for the order of the captured group. i.e. \\1
will the first captured group, \\4
the 4th one and so on. The backreference for the last one (\\7
) will include all the other characters after the 6th word.
Upvotes: 3
Reputation: 388862
We can use word
function from stringr
package which extracts words from a sentence according to the position given.
library(stringr)
word(ms, c(1,4,2,5,3,6))
#[1] "DC," "Wash," "MD," "Balt," "MA," "Bos"
If we need them back as single string we can wrap them up in paste
with collapse = ""
paste(word(ms, c(1,4,2,5,3,6)), collapse = "")
#[1] "DC,Wash,MD,Balt,MA,Bos"
Upvotes: 4