How to convert a vector of strings to Title Case

前端 未结 5 1224
长情又很酷
长情又很酷 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"
    
    0 讨论(0)
  • 2021-01-11 19:15

    I'll throw one more into the mix for fun:

    topropper(strings)
    [1] "First Phrase"              "Another Phrase To Convert" "And Here's Another One"   
    [4] "Last-one"  
    
    topropper <- function(x) {
      # Makes Proper Capitalization out of a string or collection of strings. 
      sapply(x, function(strn)
       { s <- strsplit(strn, "\\s")[[1]]
           paste0(toupper(substring(s, 1,1)), 
                 tolower(substring(s, 2)),
                 collapse=" ")}, USE.NAMES=FALSE)
    }
    
    0 讨论(0)
  • 2021-01-11 19:16

    Here is another one-liner, based on stringr package:

    str_to_title(strings, locale = "en")
    

    where strings is your vector of strings.

    Source

    0 讨论(0)
  • 2021-01-11 19:19

    Without using regex, the help page for tolower has two example functions that will do this.

    The more robust version is

    capwords <- function(s, strict = FALSE) {
        cap <- function(s) paste(toupper(substring(s, 1, 1)),
                      {s <- substring(s, 2); if(strict) tolower(s) else s},
                                 sep = "", collapse = " " )
        sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
    }
    capwords(c("using AIC for model selection"))
    ## ->  [1] "Using AIC For Model Selection"
    

    To get your regex approach (almost) working you need to set `perl = TRUE)

    gsub("(\\b[a-z]{1})", "\\U\\1" ,strings, perl=TRUE)
    
    
    [1] "First Phrase"              "Another Phrase To Convert"
    [3] "And Here'S Another One"    "Last-One"  
    

    but you will need to deal with apostrophes slightly better perhaps

    sapply(lapply(strsplit(strings, ' '), gsub, pattern = '^([[:alnum:]]{1})', replace = '\\U\\1', perl = TRUE), paste,collapse = ' ')
    

    A quick search of SO found https://stackoverflow.com/a/6365349/1385941

    0 讨论(0)
  • 2021-01-11 19:29

    The main problem is that you're missing perl=TRUE (and your regex is slightly wrong, although that may be a result of flailing around to try to fix the first problem).

    Using [:lower:] instead of [a-z] is slightly safer in case your code ends up being run in some weird (sorry, Estonians) locale where z is not the last letter of the alphabet ...

    re_from <- "\\b([[:lower:]])([[:lower:]]+)"
    strings <- c("first phrase", "another phrase to convert",
                 "and here's another one", "last-one")
    gsub(re_from, "\\U\\1\\L\\2" ,strings, perl=TRUE)
    ## [1] "First Phrase"              "Another Phrase To Convert"
    ## [3] "And Here's Another One"    "Last-One"    
    

    You may prefer to use \\E (stop capitalization) rather than \\L (start lowercase), depending on what rules you want to follow, e.g.:

    string2 <- "using AIC for model selection"
    gsub(re_from, "\\U\\1\\E\\2" ,string2, perl=TRUE)
    ## [1] "Using AIC For Model Selection"
    
    0 讨论(0)
提交回复
热议问题