如何在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 AndReporting.RWasCreated 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("NhoooIsAnElearningCompany","HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices","NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"),20,replace=TRUE)输出结果x2
[1] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"[2] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[3] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[4] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[5] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[6] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[7] "NhoooIsAnElearningCompany"
[8] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[9] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[10] "NhoooIsAnElearningCompany"
[11] "NhoooIsAnElearningCompany"
[12] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[13] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[14] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[15] "NhoooIsAnElearningCompany"
[16] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[17] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[18] "HereWeCanFindFreeTutorialsAndPaidCoursesAtMinimalPrices"
[19] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
[20] "NhoooAlsoOffersManyOtherThingsLikeQualityPdfsAndLiveCompilers"
示例
gsub("([a-z])([A-Z])","\\1 \\2",x2)输出结果
[1] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"[2] "Nhooo 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] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[5] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[6] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[7] "Nhooo Is An Elearning Company"
[8] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[9] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[10] "Nhooo Is An Elearning Company"
[11] "Nhooo Is An Elearning Company"
[12] "Here We Can Find Free Tutorials And Paid Courses At Minimal Prices"
[13] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[14] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[15] "Nhooo 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] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
[20] "Nhooo Also Offers Many Other Things Like Quality Pdfs And Live Compilers"
以上是 如何在R中的字符串向量中以大写字母开头的单词之间放置空格? 的全部内容, 来源链接: utcz.com/z/314355.html