如何删除R中字符串中特定长度的单词? [英] how to remove words of specific length in a string in R?
本文介绍了如何删除R中字符串中特定长度的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除字符串中长度小于3的单词.例如我的输入是
I want to remove words of length less than 3 in a string. for example my input is
str<- c("hello RP have a nice day")
我希望输出为
str<- c("hello have nice day")
请帮助
推荐答案
尝试一下:
gsub('\\b\\w{1,2}\\b','',str)
[1] "hello have nice day"
编辑 \ b是单词边界.如果需要减少多余的空间,请将其更改为:
EDIT \b is word boundary. If need to drop extra space,change it as:
gsub('\\b\\w{1,2}\\s','',str)
或
gsub('(?<=\\s)(\\w{1,2}\\s)','',str,perl=T)
这篇关于如何删除R中字符串中特定长度的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文