In this section, we cover how to set the case of a string.
There are four common case settings:
We wish to set the case of a string column (or literal) to lower case.
In this example, we wish to set the case of all the individual string values in the string column col_1
to lower case.
df_2 = df %>%
mutate(
col_2 = str_to_lower(col_1)
)
Here is how this works:
str_to_lower()
function from the stringr
package (part of the tidyverse
) to set the case of all letters in each string element in the column col_1
to lower case.str_to_lower()
function takes one argument which is the string column (or individual string literal) whose case is to be set to lower case.df_2
will have the same number of rows as the original data frame df
but with an additional columncol_2
holding an all lower case version of the column col_1
.We wish to set the case of a string column (or literal) to upper case.
In this example, we wish to set the case of all the individual string values in the string column col_1
to upper case.
df_2 = df %>%
mutate(
col_2 = str_to_upper(col_1)
)
Here is how this works:
str_to_upper()
function from the stringr
package (part of the tidyverse
) to set the case of all letters in each string element in the column col_1
to upper case.str_to_upper()
function takes one argument which is the string column (or individual string literal) whose case is to be set to upper case.df_2
will have the same number of rows as the original data frame df
but with an additional columncol_2
holding an all upper case version of the column col_1
.We wish to set the case of a string column (or literal) to title capitalization. Title capitalization is where the first character of each word is upper case and the rest is lower case e.g. "String Formatting".
In this example, we wish to set the case of all the individual string values in the string column col_1
to title case.
df_2 = df %>%
mutate(
col_2 = str_to_title(col_1)
)
Here is how this works:
str_to_title()
function from the stringr
package (part of the tidyverse
) to set the case of all letters in each string element in the column col_1
to title case.str_to_title()
function takes one argument which is the string column (or individual string literal) whose case is to be set to title case.df_2
will have the same number of rows as the original data frame df
but with an additional columncol_2
holding a title case version of the column col_1
.We wish to set the case of a string column (or literal) to sentence capitalization. Sentence capitalization is where the first character of the first word is upper case and the rest is lower case e.g. "String formatting is cool.".
In this example, we wish to set the case of all the individual string values in the string column col_1
to sentence case.
df_2 = df %>%
mutate(
col_2 = str_to_sentence(col_1)
)
Here is how this works:
str_to_sentence()
function from the stringr
package (part of the tidyverse
) to set the case of all letters in each string element in the column col_1
to sentence case.str_to_sentence()
function takes one argument which is the string column (or individual string literal) whose case is to be set to sentence case.df_2
will have the same number of rows as the original data frame df
but with an additional columncol_2
holding a sentence case version of the column col_1
.