77

Is there a other version to make the first letter of each string capital and also with FALSE for flac perl?

name<-"hallo"
gsub("(^[[:alpha:]])", "\\U\\1", name, perl=TRUE)
Jack Wasey
  • 3,360
  • 24
  • 43
Klaus
  • 1,946
  • 3
  • 19
  • 34

7 Answers7

113

You can try something like:

name<-"hallo"
paste(toupper(substr(name, 1, 1)), substr(name, 2, nchar(name)), sep="")

Or another way is to have a function like:

firstup <- function(x) {
  substr(x, 1, 1) <- toupper(substr(x, 1, 1))
  x
}

Examples:

firstup("abcd")
## [1] Abcd

firstup(c("hello", "world"))
## [1] "Hello" "World"
s_baldur
  • 29,441
  • 4
  • 36
  • 69
alko989
  • 7,688
  • 5
  • 39
  • 62
  • This I also watched out but it looks not so flexible if something will change later – Klaus Aug 29 '13 at 11:54
  • 23
    @Klaus but it *exactly* answers the question you posted. It is *really* bad form to change the parameters of the question *after* someone has posted a working answer. Not cool/fair! Ask a new question. – Simon O'Hanlon Aug 29 '13 at 11:57
  • 2
    Also sometimes it will be necessary to have all other chars except first in lower case. So, adding "x <- tolower(x)" will be useful – Andrii Sep 27 '18 at 15:13
78

As pointed out in the comment, it is now possible to do: stringr::str_to_title("iwejofwe asdFf FFFF")

stringr uses stringi under the hood which takes care of complex internationalization, unicode, etc., you can do: stri_trans_totitle("kaCk, DSJAIDO, Sasdd.", opts_brkiter = stri_opts_brkiter(type = "sentence"))

There is a C or C++ library underneath stringi.

Jack Wasey
  • 3,360
  • 24
  • 43
  • 15
    Now there is a stringr wrapper: `str_to_title` – fikovnik Apr 28 '17 at 10:07
  • 1
    This does not answer the question, as `str_to_title` converts **every letter in the string to lowercase, except the first letter of each word**. E.g., `str_to_title("AB ABC abc")` returns `Ab Abc Abc`. So, among other things, it destroys acronyms. – Antoine May 27 '23 at 14:37
32

In stringr, there's str_to_sentence() which does something similar. Not quite an answer to this question, but it solves the problem I had.

str_to_sentence(c("not today judas", "i love cats", "other Caps converteD to lower though"))
#> [1] "Not today judas"  "I love cats"  "Other caps converted to lower though"  
Oliver
  • 1,098
  • 1
  • 11
  • 16
  • 2
    Can't understand why this was downvoted. Exactly what I was looking for. Upvoted it. – TobiSonne Jul 17 '20 at 15:43
  • 3
    Problem with str_to_sentence is that it will turn eg: EFFG-1 to Effg-1 while str_replace(input, "^\\w{1}", toupper) keeps the original structure if multiple letters are uppercase. For eg gene names this is a better solution. – Sebastian Hesse Feb 19 '22 at 13:12
19

for the lazy typer:

  paste0(toupper(substr(name, 1, 1)), substr(name, 2, nchar(name)))

will do too.

irJvV
  • 892
  • 8
  • 26
  • 2
    If by “super lazy” you mean “knows that `paste0(x)` exists and is equivalent to `paste(x, sep = '')`”. – Konrad Rudolph Sep 24 '15 at 10:27
  • @KonradRudolph you are 100% right -> With lazy i mean just less code to type and maintain. (can make quite a difference if you start concatenating code into one Functional Programming line - like I do a lot - ) :^) – irJvV Sep 24 '15 at 12:50
  • And paste0 is faster. – RHA Oct 23 '15 at 08:47
  • This answers the question and uses base R. Should be the accepted answer IMO. – Antoine May 27 '23 at 14:41
9

I like the "tidyverse" way using stringr with a oneliner

library(stringr)
input <- c("this", "is", "a", "test")
str_replace(input, "^\\w{1}", toupper)

Resulting in:

[1] "This" "Is"   "A"    "Test"
Jurgen Van Impe
  • 181
  • 2
  • 2
8

Often we want only the first letter upper case, rest of the string lower case. In such scenario, we need to convert the whole string to lower case first.

Inspired by @alko989 answer, the function will be:

firstup <- function(x) {
  x <- tolower(x)
  substr(x, 1, 1) <- toupper(substr(x, 1, 1))
  x
}

Examples:

firstup("ABCD")
## [1] Abcd

Another option is to use str_to_title in stringr package

dog <- "The quick brown dog"    
str_to_title(dog)
## [1] "The Quick Brown Dog"
laviex
  • 593
  • 7
  • 13
2

I was also looking for the answer on this, but the replies did not consider the case of strings starting with numbers or symbols. Also, the question was: "make the first letter of each string capital", not every word.

v = c("123.1 test test", "test", "test test", ". test")
stringr::str_replace(v, "([[:alpha:]])", toupper)
## [1] "123.1 Test test" "Test"            "Test test"       ". Test"  
felixsuett
  • 21
  • 1