String transformation in R | Grouping words of a string

后端 未结 5 1232
孤街浪徒
孤街浪徒 2021-01-20 10:02

I want to group the words of string(given below)

text=\"Lorem,ipsum,dolor,sit,amet,consectetuer\"

like this

textNew=\"Lore         


        
5条回答
  •  旧时难觅i
    2021-01-20 10:39

    Through gsub function,

    > text="Lorem,ipsum,dolor,sit,amet,consectetuer"
    > f <- gsub(",([^,]*)", " \\1,\\1", text, perl=TRUE)
    > result <- gsub(",[^,]*$", "", f, perl=TRUE)
    > result
    [1] "Lorem ipsum,ipsum dolor,dolor sit,sit amet,amet consectetuer"
    

提交回复
热议问题