<- "hello, world!"
text <- toupper(text)
result print(result)
[1] "HELLO, WORLD!"
# Output: "HELLO, WORLD!"
Steven P. Sanderson II, MPH
August 6, 2024
Greetings, useR! Today, we’re exploring a handy function from base R that will help with string manipulation: toupper()
. This little function is the complement to tolower()
which I have previously written about. Let’s take a look!
toupper()
all about?At its core, toupper()
does one thing exceptionally well: it converts all lowercase letters in a string to uppercase. It’s straightforward, efficient, and incredibly versatile in various scenarios.
Where x
is the character vector you want to convert to uppercase.
Let’s dive into some practical examples to see toupper()
in action!
[1] "HELLO, WORLD!"
In this example, we transform a simple greeting into all caps. Notice how toupper()
affects only the letters, leaving punctuation and spaces untouched.
[1] "APPLE" "BANANA" "CHERRY"
Here, we apply toupper()
to a vector of fruit names. It handles each element separately, converting all to uppercase.
[1] "R IS AWESOME! IT'S 2024 :)"
This example showcases how toupper()
deals with mixed case text and special characters. It converts lowercase to uppercase but leaves already uppercase letters, numbers, and symbols as they are.
You can easily combine toupper()
with other string functions for more complex operations. For instance:
[1] "R PROGRAMMING IS FUN"
Here, we first trim whitespace with trimws()
, then convert to uppercase.
toupper()
?I encourage you to open your R console and experiment with toupper()
! Try it on different types of strings, combine it with other functions, and see how it can enhance your text processing workflows.
Remember, toupper()
is just one of many string manipulation functions in R. As you become more comfortable with it, explore other functions like tolower()
, chartr()
, and substr()
to expand your text processing toolkit.
Happy coding, and may your strings always be perfectly cased!