如何在 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"

更新於: 10-Feb-2021

1 千+ 瀏覽

啟動你的職業生涯

完成課程即獲得認證

開始學習
廣告