the_darkside the_darkside - 4 months ago 6
R Question

reducing whitespace to 1 space between words

I have a list of facebook posts from which I have removed symbology. Now I am left with gaps between the text - 2 or more spaces, which I would like to condense. How can I remove the extra whitespace so that there is only one space between words? Also, how could one remove all capital letters that stand by themselves in the text?

> head(posts)
[1] "Syntel Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Syntel Registration Link"
[2] "Dont Miss This Opportunity to be get placed in one of the best MNC companies in the world eBay freshers this week of January 2016 Qualification Any Graduate Can Apply eBay Registration Link"
[3] "Recent Pass Outs with 55 or More are eligible to Apply in Wipro Go to the Updated Link for LastDay Reference Drive Jan 2016 Apply Link for Fresher Referral Apply Link"
[4] "Robert Bosch Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Robert Bosch Registration Link"
[5] "Mega JOB OPENINGS OF THE YEAR Mphasis Recruitment for FRESHERS January 2016 Qualification BE B Tech B Sc BCA Any Graduates MCA MBA ME M Tech Post Graduates Mphasis Registration Link"
[6] "TRIGENT Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Trigent Registration Link"


> dput(head(posts))
c("Syntel Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Syntel Registration Link",
"Dont Miss This Opportunity to be get placed in one of the best MNC companies in the world eBay freshers this week of January 2016 Qualification Any Graduate Can Apply eBay Registration Link",
"Recent Pass Outs with 55 or More are eligible to Apply in Wipro Go to the Updated Link for LastDay Reference Drive Jan 2016 Apply Link for Fresher Referral Apply Link",
"Robert Bosch Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Robert Bosch Registration Link",
"Mega JOB OPENINGS OF THE YEAR Mphasis Recruitment for FRESHERS January 2016 Qualification BE B Tech B Sc BCA Any Graduates MCA MBA ME M Tech Post Graduates Mphasis Registration Link",
"TRIGENT Recruitment Drive in this week for FRESHERS New Registration Link 2016 for 2013 2014 2015 Passout Graduates Qualification Any Graduate B E B Tech MCA M E M Tech Trigent Registration Link"
)

lmo lmo
Answer

using gsub, you could try

posts <- gsub(" +", " ", posts)

This will replace every set of adjacent spaces with a single space.