How to put space between words that start with uppercase letter in string vector in R?

R ProgrammingServer Side ProgrammingProgramming

To put space between words that start with uppercase letter in string vector in R, we can use gsub function. Since we can have uppercase letters as well as lowercase letters in the string vector hence, we need to properly specify both type of characters for creating the space between words. Check out the below examples to understand how it works.

Example

 Live Demo

x1<-c("RIsAProgrammingLanguageAndSoftwareEnvironmentForStatisticalAnalysis,GraphicsRepresentationAndReporting.RWasCreatedByRossIhakaAndRobertGentlemanAtTheUniversityOfAuckland,NewZealand,AndIsCurrentlyDevelopedByTheRDevelopmentCoreTeam. RIsFreelyAvailableUnderTheGNUGeneralPublicLicense,AndPre-compiledBinaryVersionsAreProvidedForVariousOperatingSystemsLikeLinux,WindowsAndMac. ThisProgrammingLanguageWasNamedR,BasedOnTheFirstLetterOfFirstNameOfTheTwoRAuthors(RobertGentlemanAndRossIhaka),AndPartlyAPlayOnTheNameOfTheBellLabsLanguage S.")
x1

Output

[1] "RIsAProgrammingLanguageAndSoftwareEnvironmentForStatisticalAnalysis,GraphicsRepresentationAndReporting.RWasCreatedByRossIhakaAndRobertGentlemanAtTheUniversityOfAuckland,NewZealand,AndIsCurrentlyDevelopedByTheRDevelopmentCoreTeam. RIsFreelyAvailableUnderTheGNUGeneralPublicLicense,AndPre-compiledBinaryVersionsAreProvidedForVariousOperatingSystemsLikeLinux,WindowsAndMac. ThisProgrammingLanguageWasNamedR,BasedOnTheFirstLetterOfFirstNameOfTheTwoRAuthors(RobertGentlemanAndRossIhaka),AndPartlyAPlayOnTheNameOfTheBellLabsLanguage S."

Example

gsub("([a-z])([A-Z])","\\1 \\2",x1)

Output

[1] "RIs AProgramming Language And Software Environment For Statistical Analysis,Graphics Representation And Reporting.RWas Created By Ross Ihaka And Robert Gentleman At The University Of Auckland,New Zealand,And Is Currently Developed By The RDevelopment Core Team. RIs Freely Available Under The GNUGeneral Public License,And Pre-compiled Binary Versions Are Provided For Various Operating Systems Like Linux,Windows And Mac. This Programming Language Was Named R,Based On The First Letter Of First Name Of The Two RAuthors(Robert Gentleman And Ross Ihaka),And Partly APlay On The Name Of The Bell Labs Language S."

Example

 Live Demo

x2<-sample(c("TutorialspointIsAnElearningCompany","HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices","TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"),20,replace=TRUE)
x2

Output

[1] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[2] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[3] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[4] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[5] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[6] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[7] "TutorialspointIsAnElearningCompany"
[8] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[9] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[10] "TutorialspointIsAnElearningCompany"
[11] "TutorialspointIsAnElearningCompany"
[12] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[13] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[14] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[15] "TutorialspointIsAnElearningCompany"
[16] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[17] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[18] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[19] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[20] "TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"

Example

gsub("([a-z])([A-Z])","\\1 \\2",x2)

Output

[1] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[2] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[3] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[4] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[5] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[6] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[7] "Tutorialspoint Is An Elearning Company"
[8] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[9] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[10] "Tutorialspoint Is An Elearning Company"
[11] "Tutorialspoint Is An Elearning Company"
[12] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[13] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[14] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[15] "Tutorialspoint Is An Elearning Company"
[16] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[17] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[18] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[19] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[20] "Tutorialspoint Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
raja
Published on 10-Feb-2021 11:01:05
Advertisements