str_to_title() function in R – Practical Guide!

Filed Under: R Programming
R Str To Title() Function

Hello, readers! In this article, we will be focusing on R str_to_title() function in detail.

So, let us begin!!


Usage of str_to_title() in R

Like any other programming language, R programming provides us with various functions to perform string manipulations.

String manipulations comes into picture especially when you deal with datasets that contain character data that needs to be transformed.

The str_to_title() function in R accepts a string value as input, and converts the first alphabet of every word of the string to Upper Case format.

So lets say str = “welcome home.” would turn out to be ‘Welcome Home’.

Only the first letter of every word gets converted to Uppercase.

Thus, in a Nutshell, we can say that the str_to_title() function performs the following tasks:

  • Converts every first letter of the word to Uppercase.
  • Converts all other letters of the word to lowercase.

Have a look at the below syntax!

str_to_title(string)

Example 1: R str_to_title() on a string

R stringr library provides us with str_to_title() to work on the string data. Thus, we need to install and load the library into the R environment as shown below-

Example:

#Removed all the existing objects
rm(list = ls())
#install.packages('stringr')
library(stringr) 

# Creating a string 
data <- "welcome to journalDev!"

print(str_to_title(data)) 

Output:

"Welcome To Journaldev!"

Example 2: R str_to_title() with word() function

In this example, we follow the below steps:

  • Initially, we load the stringr library into the R environment.
  • We accept a string as input (hard-coded).
  • Using word() function, we extract a word at position 3 from the input string.
  • Then, we feed the extracted word to the str_to_title() function, which in turn converts the first alphabet of the word to Uppercase.

Example:

#Removed all the existing objects
rm(list = ls())
#install.packages('stringr')
library(stringr) 

# Creating a string 
data <- "welcome to journalDev!"
wrd = word(data, 3) 
print(str_to_title(wrd))

Output:

"Journaldev!"

Example 3: R str_to_title() function with all Uppercase string

In this example, we feed the str_to_title() function with a string that has all the words of Uppercase letters.

In this scenario, the function retains the Uppercase property of the first alphabet of every word and also converts rest of the alphabets of every word to lower case.

Example:

#Removed all the existing objects
rm(list = ls())
#install.packages('stringr')
library(stringr) 

# Creating a string 
data <- "WELCOME TO JOURNALDEV!"

print(str_to_title(data)) 

Output:

"Welcome To Journaldev!"

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

On a broader level, the str_to_title() function in R can be used to manipulate the string values of columns of huge datasets in a single go. This would save time and bring efficiency in the analysis.

Recommend you to implement the concept of str_to_title() function with various sets of strings and analyze the output.

Do let us know your experience about the analysis in the comment section.

For more such posts related to R programming, stay tuned.

Till then, Happy Learning! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages