R Advanced

Converting First letter of Every Word to Uppercase in R

When you are working with large datasets, you often come across names that are in inconsistent formats or capitalization. To solve this problem, we need to standardize the format to improve data quality and consistency. One way to standardize is by converting the first letter of each word to uppercase.

The most efficient way to convert the first letter of every word to uppercase in R is by using the “str_to_title()” function from the “stringr” package. It accepts a string as an argument and returns it with each first letter of every word turned into uppercase.

Below is the figure that describes what I mean:

Here is the step-by-step guide:

Step 1: Load the stringr package

The str_to_title() function is not a base R function, and it is a function from the “stringr” package.

library(stringr)

Step 2: Define a string you want to convert

In this step, you must define a string whose letter you want to convert.

main_string <- "i met doctor yesterday!"

Step 3: Use the str_to_title() function

Pass the main_string to the str_to_title() function.

converted_str <- str_to_title(main_string)

Our final code looks like this:

library(stringr)

main_string <- "i met doctor yesterday!"

converted_str <- str_to_title(main_string)

print(converted_str) # I Met Doctor Yesterday!

String with mixed case

Let’s take an example of a string with a mixed case.

library(stringr)

main_string <- "i MeT doCTor yesTerdaY!"

converted_str <- str_to_title(main_string)

print(converted_str) # I Met Doctor Yesterday!

You can see that the function converts any existing uppercase letters within words to lowercase before capitalizing the first letter.

String with numbers and symbols

Let’s take a complex string that contains letters, numbers, and symbols and see the outcome.

library(stringr)

complex_string <- "123 @xFRDW c22() m**&"

converted_str <- str_to_title(complex_string)

print(converted_str) # 123 @Xfrdw C22() M**&

You can see from the above program that it doesn’t modify numbers or symbols within the string.

Vector of strings

If you want to convert multiple strings at once, you can create a vector of it and pass it to the str_to_title() function.

library(stringr)

vec <- c("first string", "second string", "third string")

converted_vec <- str_to_title(vec)

print(converted_vec) # "First String" "Second String" "Third String"

That’s all!

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

2 weeks ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

2 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

3 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

4 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

2 months ago