Skip to main content
v2026.11,858 entries · CC-BY 4.0

R Uppercase: Convert Text to Upper Case with toupper() and stringr

How to convert text to upper case in R with toupper(), casefold() and stringr, including the locale behaviour that changes the result.

Written and maintained by CASRAI Editorial Board

Last updated

To convert text to upper case in R, call toupper(x). It is part of base R, needs no package, and returns a character vector the same length as x. For locale-aware casing, the stringr function str_to_upper(string, locale = "en") does the same job and lets you name the locale. This page covers the exact behaviour of both, including locale-sensitive cases such as the Turkish letter i.

What it is used for in research

Upper-casing is a data-cleaning step. Identifiers, sample labels, country codes and gene or instrument symbols often arrive in mixed case across files, and a join or match on text is case-sensitive in R. Converting both sides to one case first makes values such as “ab12” and “AB12” compare as equal. It also standardises labels for tables and figure legends. Put the conversion in a script rather than editing cells by hand so the step is recorded; see data wrangling for where text cleaning fits in a workflow.

Install or access (official source only)

toupper(), tolower(), casefold() and chartr() are documented together on one page of the R base manual and ship with R, so there is nothing to install. R itself is distributed through CRAN, the Comprehensive R Archive Network.

The stringr functions come from the stringr package in the tidyverse. Install it once and load it in each session:

install.packages("stringr")
library(stringr)

Step-by-step: convert text to upper case

  1. Convert a string with toupper(). The R manual’s own example uses x <- "MiXeD cAsE 123"; toupper(x) returns "MIXED CASE 123". Digits and other non-alphabetic characters are left unchanged.
  2. Convert a whole vector or data frame column. toupper() accepts a character vector, or an object that can be coerced to character with as.character, and returns a character vector of the same length. To overwrite a column: df$id <- toupper(df$id).
  3. Use casefold() if you prefer one function for both directions. casefold(x, upper = TRUE) translates to upper case; with the default upper = FALSE it translates to lower case. casefold is a wrapper, so casefold(x, upper = TRUE) behaves like toupper(x).
  4. Use stringr when you need a locale. With the package loaded, dog <- "The quick brown dog" and str_to_upper(dog) give "THE QUICK BROWN DOG". The same package offers str_to_lower(), str_to_title() ("The Quick Brown Dog") and str_to_sentence(), which turns "the quick brown dog" into "The quick brown dog".
  5. Capitalise only the first letter. Base R has no single function for this; the R manual’s examples build “Mixed Case” output by applying toupper() to the first character taken with substring(). In stringr, str_to_sentence() and str_to_title() cover the common cases.

Output and how to read it

The result is a character vector with the same length and attributes as the input after coercion. Only letters change; digits, spaces and punctuation come back as they went in. If you pass a factor, the coercion to character means you get a character vector back, not a factor, so convert it back with factor() if later code expects levels.

Troubleshooting

Accented or non-English letters do not change

The R manual states that these functions are platform-dependent and usually rely on operating-system services, which can be limited, for example covering only ASCII characters in 8-bit locales. What counts as an alphabetic character also depends on the platform. If accented letters stay lower case, check your session’s locale and encoding first.

The result is not what a language expects (Turkish i)

Casing rules differ by language. The stringr reference shows str_to_upper("i") returning "I", while str_to_upper("i", "tr") returns "İ", the dotted capital used in Turkish. The default locale is "en", set so that behaviour is consistent across platforms; other values come from stringi::stri_locale_list().

Character count changes

The R manual notes that more than one character can map to a single upper-case character, so do not assume a mapping is always one-to-one when you compare string lengths before and after.

You passed something that is not text

Numbers, factors and other objects are coerced with as.character first. Check class(x) if a result looks surprising.

Free alternatives

Function Package Locale argument Notes
toupper(x) base R No Platform-dependent; uses OS services
casefold(x, upper = TRUE) base R No Wrapper that behaves like toupper(x)
str_to_upper(string, locale = "en") stringr Yes Default "en" keeps behaviour consistent across platforms
str_to_title(), str_to_sentence() stringr Yes Title case and sentence case instead of all capitals

All of the above are free and open source. For working with the rest of a cleaned data set, see histograms in R and maps in R for plotting once labels are standardised.

FAQ

How do I make a string upper case in R?

Use toupper("text"), which returns "TEXT". It works on a single string or an entire character vector.

What is the difference between toupper() and casefold()?

casefold(x, upper = TRUE) is a wrapper and behaves like toupper(x). The advantage is a single function with an upper switch.

How do I capitalise only the first letter in R?

Base R has no single function for it; the R manual’s examples apply toupper() to the first character of each word taken with substring(). In stringr, str_to_sentence() and str_to_title() cover the common cases.

Why does str_to_upper("i") return a different letter in Turkish?

Because casing rules depend on language. str_to_upper("i") returns "I", while str_to_upper("i", "tr") returns "İ".

Does upper-casing in R depend on my computer?

Yes for base R: the manual describes the functions as platform-dependent. stringr’s str_to_upper() takes a locale argument with default "en" for consistent results.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about R Uppercase: Convert Text to Upper Case with toupper() and stringr

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.