Often you may want to convert all of the characters in a string in R to uppercase.
The easiest way to do so is by using the toupper() function from base R, which is designed to perform this exact task.
The toupper() function uses the following basic syntax:
toupper(x)
where:
- x: Name of string or vector to convert to uppercase.
The following examples show how to use the toupper() function in practice in several different scenarios.
Example 1: Convert a Single String to Uppercase
Suppose that we would like to convert a single string in R to all uppercase.
The following example shows how to do so using the toupper() function:
#create string my_string <- 'This is a string that contains Many Words.' #convert string to all uppercase new_string <- toupper(my_string) #view new string new_string [1] "THIS IS A STRING THAT CONTAINS MANY WORDS."
We can see that the toupper() function has successfully converted all characters in the initial string to all uppercase characters in the new string.
Example 2: Convert Each String in Column to Uppercase
Suppose that we would like to convert each string in a column of a data frame to all uppercase letters.
First, let’s create the following data frame to work with:
#create data frame
df <- data.frame(team=c('Mavs', 'Nets', 'Hawks', 'Heat', 'Kings'),
points=c(99, 68, 86, 88, 95),
assists=c(22, 28, 45, 28, 31),
rebounds=c(30, 28, 36, 30, 29))
#view data frame
df
team points assists rebounds
1 Mavs 99 22 30
2 Nets 68 28 28
3 Hawks 86 45 36
4 Heat 88 28 30
5 Kings 95 31 29
Suppose that we would like to convert each string in the team column of the data frame to all uppercase letters.
We can use the following syntax with the toupper() function to do so:
#convert all strings in team column to uppercase
df$team <- toupper(df$team)
#view updated data frame
df
team points assists rebounds
1 MAVS 99 22 30
2 NETS 68 28 28
3 HAWKS 86 45 36
4 HEAT 88 28 30
5 KINGS 95 31 29
Notice that each string in the team column of the data frame is now shown in all uppercase letters.
Example 3: Convert Strings in Multiple Columns to Uppercase
Suppose that we would like to convert the strings in multiple columns of a data frame to all uppercase.
First, let’s create the following data frame to work with:
#create data frame
df <- data.frame(team=c('Mavs', 'Nets', 'Hawks', 'Heat', 'Kings'),
conf=c('West', 'East', 'East', 'East', 'West'),
assists=c(22, 28, 45, 28, 31),
rebounds=c(30, 28, 36, 30, 29))
#view data frame
df
team conf assists rebounds
1 Mavs West 22 30
2 Nets East 28 28
3 Hawks East 45 36
4 Heat East 28 30
5 Kings West 31 29
Now suppose that we would like to convert each string in the team and conf columns to all uppercase letters.
We can use the following syntax with the toupper() function to do so:
#convert team and conference to uppercase
df[c('team', 'conf')] <- sapply(df[c('team', 'conf')], function(x) toupper(x))
#view updated data frame
df
team conf assists rebounds
1 MAVS WEST 22 30
2 NETS EAST 28 28
3 HAWKS EAST 45 36
4 HEAT EAST 28 30
5 KINGS WEST 31 29
Notice that all strings in both the team and conf columns of the data frame have been converted to uppercase letters.
Additional Resources
The following tutorials explain how to perform other common tasks related to strings in R:
How to Use str_split in R
How to Use str_replace in R
How to Count Words in String in R
How to Convert a Vector to String in R