How to convert a vector of strings to Title Case

前端 未结 5 1262
长情又很酷
长情又很酷 2021-01-11 19:00

I have a vector of strings in lower case. I\'d like to change them to title case, meaning the first letter of every word would be capitalized. I\'ve managed to do it with a

5条回答
  •  余生分开走
    2021-01-11 19:04

    Already excellent answers here. Here's one using a convenience function from the reports package:

    strings <- c("first phrase", "another phrase to convert",
        "and here's another one", "last-one")
    
    CA(strings)
    
    ## > CA(strings)
    ## [1] "First Phrase"              "Another Phrase To Convert"
    ## [3] "And Here's Another One"    "Last-one"       
    

    Though it doesn't capitalize one as it didn't make sense to do so for my purposes.

    Update I manage the qdapRegex package that has the TC (title case) function that does true title case:

    TC(strings)
    
    ## [[1]]
    ## [1] "First Phrase"
    ## 
    ## [[2]]
    ## [1] "Another Phrase to Convert"
    ## 
    ## [[3]]
    ## [1] "And Here's Another One"
    ## 
    ## [[4]]
    ## [1] "Last-One"
    

提交回复
热议问题