如何在 R 中的字符串矢量中在以大写字母开头的单词之间添加空格?
若要给 R 中的字符串矢量中以大写字母开头的单词之间添加空格,可以使用 gsub 函数。由于在字符串矢量中可能同时出现大写字母和小写字母,因此我们需要正确指定两种类型的字符以在单词之间创建空格。查看以下示例以了解其工作原理。
示例
x1<-c("RIsAProgrammingLanguageAndSoftwareEnvironmentForStatisticalAnalysis,GraphicsRepresentationAndReporting.RWasCreatedByRossIhakaAndRobertGentlemanAtTheUniversityOfAuckland,NewZealand,AndIsCurrentlyDevelopedByTheRDevelopmentCoreTeam. RIsFreelyAvailableUnderTheGNUGeneralPublicLicense,AndPre-compiledBinaryVersionsAreProvidedForVariousOperatingSystemsLikeLinux,WindowsAndMac. ThisProgrammingLanguageWasNamedR,BasedOnTheFirstLetterOfFirstNameOfTheTwoRAuthors(RobertGentlemanAndRossIhaka),AndPartlyAPlayOnTheNameOfTheBellLabsLanguage S.") x1
输出
[1] "RIsAProgrammingLanguageAndSoftwareEnvironmentForStatisticalAnalysis,GraphicsRepresentationAndReporting.RWasCreatedByRossIhakaAndRobertGentlemanAtTheUniversityOfAuckland,NewZealand,AndIsCurrentlyDevelopedByTheRDevelopmentCoreTeam. RIsFreelyAvailableUnderTheGNUGeneralPublicLicense,AndPre-compiledBinaryVersionsAreProvidedForVariousOperatingSystemsLikeLinux,WindowsAndMac. ThisProgrammingLanguageWasNamedR,BasedOnTheFirstLetterOfFirstNameOfTheTwoRAuthors(RobertGentlemanAndRossIhaka),AndPartlyAPlayOnTheNameOfTheBellLabsLanguage S."
示例
gsub("([a-z])([A-Z])","\1 \2",x1)
输出
[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."
示例
x2<-sample(c("TutorialspointIsAnElearningCompany","HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices","TutorialspointAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"),20,replace=TRUE) x2
输出
[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"
示例
gsub("([a-z])([A-Z])","\1 \2",x2)
输出
[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"
广告