0

I have several columns that have values that are a mix of lowercase, uppercase, etc., letters. I'd like for the first letter of a word to be uppercase and the letters after to be lowercase.

An example of what I'd like:

phoenix        Phoenix
Little rock    Little Rock
AUGUSTA        Augusta
purple_plop
  • 280
  • 1
  • 10

2 Answers2

2

We could use gsub

gsub("\\b([a-z])", "\\U\\1", tolower(v1), perl = TRUE)
#[1] "Phoenix"     "Little Rock" "Augusta" 

data

v1 <- c("phoenix", "Little rock", "AUGUSTA")
akrun
  • 874,273
  • 37
  • 540
  • 662
2

You can use str_to_title from the stringr package:

library(stringr)

str_to_title(c("phoenix", "Little rock", "AUGUSTA"))
# [1] "Phoenix"     "Little Rock" "Augusta"  
Junitar
  • 905
  • 6
  • 13