How can I convert strings to lowercase in R, and can you provide examples?

How can I convert strings to lowercase in R, and can you provide examples?

To convert strings to lowercase in R, the function “tolower()” can be used. This function will take a string as input and return a new string with all characters converted to lowercase. For example, if the input string is Hello World”, the output string would be “hello world”. This function is useful for standardizing text data or for performing case-insensitive comparisons. It can be applied to individual strings or to columns in a data frame.

Convert Strings to Lowercase in R (With Examples)


You can use the built-in tolower() function in R to convert strings to lowercase.

#convert string to lowercase
tolower(string_name)

The following examples show how to use this function in practice.

Example 1: Convert a Single String to Lowercase

The following code shows how to convert a single string to lowercase in R:

#create string
my_string <- 'THIS IS A SENTENCE WITH WORDS.'

#convert string to all lowercase
tolower(my_string)

[1] "this is a sentence with words."

Note that the tolower() function converts all characters in a string to lowercase

Example 2: Convert Each String in Column to Lowercase

The following code shows how to convert every string in a column of a data frame to lowercase:

#create data frame
df <- data.frame(team=c('Mavs', 'Nets', 'Spurs'),
                 points=c(99, 94, 85),
                 rebounds=c(31, 22, 29))

#view data frame
df

   team points rebounds
1  Mavs     99       31
2  Nets     94       22
3 Spurs     85       29

#convert team names to lowercase
df$team <- tolower(df$team)

#view updated data frame
df

   team points rebounds
1  mavs     99       31
2  nets     94       22
3 spurs     85       29

Example 3: Convert Strings in Multiple Columns to Lowercase

The following code shows how to convert strings in multiple columns of a data frame to lowercase:

#create data frame
df <- data.frame(team=c('Mavs', 'Nets', 'Spurs'),
                 conf=c('WEST', 'EAST', 'WEST'),
                 points=c(99, 94, 85))

#view data frame
df

   team conf points
1  Mavs WEST     99
2  Nets EAST     94
3 Spurs WEST     85

#convert team and conference to lowercase
df[c('team', 'conf')] <- sapply(df[c('team', 'conf')], function(x) tolower(x))

#view updated data frame
df
   team conf points
1  mavs west     99
2  nets east     94
3 spurs west     85

The following tutorials explain how to perform other common tasks related to strings in R:

Cite this article

stats writer (2024). How can I convert strings to lowercase in R, and can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-strings-to-lowercase-in-r-and-can-you-provide-examples/

stats writer. "How can I convert strings to lowercase in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-strings-to-lowercase-in-r-and-can-you-provide-examples/.

stats writer. "How can I convert strings to lowercase in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-strings-to-lowercase-in-r-and-can-you-provide-examples/.

stats writer (2024) 'How can I convert strings to lowercase in R, and can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-strings-to-lowercase-in-r-and-can-you-provide-examples/.

[1] stats writer, "How can I convert strings to lowercase in R, and can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I convert strings to lowercase in R, and can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top