Case

In this section, we cover how to set the case of a string.

There are four common case settings:

  1. Lower where all characters are lower case e.g. "lower".
  2. Upper where all characters are upper case e.g. "Upper".
  3. Title where the first character of each word is upper case and the rest is lower case e.g. "String Formatting".
  4. Sentence where the first character of the first word is upper case and the rest is lower case e.g. "String formatting is cool.".

To Lower

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:

  • We use the 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.
  • Most commonly, the 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.
  • The output data frame 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.

To Upper

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:

  • We use the 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.
  • Most commonly, the 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.
  • The output data frame 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.

To Title

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:

  • We use the 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.
  • Most commonly, the 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.
  • The output data frame 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.

To Sentence

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:

  • We use the 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.
  • Most commonly, the 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.
  • The output data frame 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.
R
I/O