R 中的字符串转换 |将字符串的单词分组 [英] String transformation in R | Grouping words of a string

查看:54
本文介绍了R 中的字符串转换 |将字符串的单词分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对字符串的单词进行分组(下面给出)

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

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

喜欢这个

textNew="Lorem ipsum,ipsum dolor,dolor sit,sit amet,amet consectetuer"

谢谢.

推荐答案

通过gsub函数,

> 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"

这篇关于R 中的字符串转换 |将字符串的单词分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆