转换为R中的句子案例

时间:2017-04-24 15:42:31

标签: r case sentencecase

我正在尝试清理R中的以下数据 我有一个字符串向量,看起来像这样 -

    /organization/-fame
    /ORGANIZATION/-QOUNTER
    /organization/-qounter
    /ORGANIZATION/-THE-ONE-OF-THEM-INC-
    /organization/0-6-com
    /ORGANIZATION/004-TECHNOLOGIES
    /organization/01games-technology
    /ORGANIZATION/0NDINE-BIOMEDICAL-INC
    /organization/0ndine-biomedical-inc
    /ORGANIZATION/0XDATA
    /organization/0xdata
    /ORGANIZATION/0XDATA
    /organization/0xdata
    /ORGANIZATION/1
    /organization/1
    /ORGANIZATION/1
    /organization/1-2-3-listo
    /ORGANIZATION/1-4-ALL
    /organization/1-618-technology
    /ORGANIZATION/1-800-DENTIST
    /organization/1-800-doctors
    /ORGANIZATION/1-800-PUBLICRELATIONS-INC-
    /organization/1-mainstream
    /ORGANIZATION/1-OF-99
    /organization/10-20-media
    /ORGANIZATION/10-20-MEDIA

我想将字符串中每个单词的大小写更改为句子大小写。所以 改变之后,一切都必须看起来像 -

    /Organization/-Fame
    /Organization/-Qounter
    /Organization/-The-One-Of-Them-Inc-
    /Organization/0-6-Com
    /Organization/004-Technologies
    /Organization/01Games-Technology
    /Organization/0Ndine-Biomedical-Inc
    /Organization/0Xdata
    /Organization/1
    /Organization/1-2-3-Listo
    /Organization/1-4-All
    /Organization/1-618-Technology
    /Organization/1-800-Dentist
    /Organization/1-800-Doctors
    /Organization/1-800-Publicrelations-Inc-
    /Organization/1-Mainstream
    /Organization/1-Of-99
    /Organization/10-20-Media

1 个答案:

答案 0 :(得分:2)

您可以使用正则表达式。使用您的示例输入

x<-c("/organization/-fame", "/ORGANIZATION/-QOUNTER", "/organization/-qounter", 
"/ORGANIZATION/-THE-ONE-OF-THEM-INC-", "/organization/0-6-com", 
"/ORGANIZATION/004-TECHNOLOGIES", "/organization/01games-technology", 
"/ORGANIZATION/0NDINE-BIOMEDICAL-INC", "/organization/0ndine-biomedical-inc", 
"/ORGANIZATION/0XDATA", "/organization/0xdata", "/ORGANIZATION/0XDATA", 
"/organization/0xdata", "/ORGANIZATION/1", "/organization/1", 
"/ORGANIZATION/1", "/organization/1-2-3-listo", "/ORGANIZATION/1-4-ALL", 
"/organization/1-618-technology", "/ORGANIZATION/1-800-DENTIST", 
"/organization/1-800-doctors", "/ORGANIZATION/1-800-PUBLICRELATIONS-INC-", 
"/organization/1-mainstream", "/ORGANIZATION/1-OF-99", "/organization/10-20-media", 
"/ORGANIZATION/10-20-MEDIA")

你可以运行

gsub("([[:alpha:]])([[:alpha:]]+)", "\\U\\1\\L\\2", x, perl=TRUE)

获取

 [1] "/Organization/-Fame"                     
 [2] "/Organization/-Qounter"                  
 [3] "/Organization/-Qounter"                  
 [4] "/Organization/-The-One-Of-Them-Inc-"     
 [5] "/Organization/0-6-Com"                   
 [6] "/Organization/004-Technologies"          
 [7] "/Organization/01Games-Technology"        
 [8] "/Organization/0Ndine-Biomedical-Inc"     
 [9] "/Organization/0Ndine-Biomedical-Inc"     
[10] "/Organization/0Xdata"                    
[11] "/Organization/0Xdata"                    
[12] "/Organization/0Xdata"                    
[13] "/Organization/0Xdata"                    
[14] "/Organization/1"                         
[15] "/Organization/1"                         
[16] "/Organization/1"                         
[17] "/Organization/1-2-3-Listo"               
[18] "/Organization/1-4-All"                   
[19] "/Organization/1-618-Technology"          
[20] "/Organization/1-800-Dentist"             
[21] "/Organization/1-800-Doctors"             
[22] "/Organization/1-800-Publicrelations-Inc-"
[23] "/Organization/1-Mainstream"              
[24] "/Organization/1-Of-99"                   
[25] "/Organization/10-20-Media"               
[26] "/Organization/10-20-Media"