Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

I want my output to be

str<- c("hello have nice day")

Please help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

gsub('\b\w{1,2}\b','',str)
[1] "hello  have  nice day"

EDIT is word boundary. If need to drop extra space,change it as:

gsub('\b\w{1,2}\s','',str)

Or

gsub('(?<=\s)(\w{1,2}\s)','',str,perl=T)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...