3 Easy Ways to Reverse a String in R [Examples]

There are many operations you can perform on a character string in R, for example, reverse them.

In this article, we show 3 easy ways to reverse a string in R.

However, depending on who you ask, reversing a string can have two interpretations. It can be either:

  1. Reversing the characters of a string, or
  2. Reversing the words in a string.

For example

[1] "summer"
[1] "remmus"

Or

[1] "it is summer"
[1] "summer is it"

In this article, we discuss both. We discuss 3 methods to reverse the characters of a string, and one method to reverse the words in a string.

How to Reverse the Characters of a String in R

In this section, we discuss 3 ways to invert the characters of a string in R.

The first and second method only use basic R code. But, for the third method, you need to install the stringi package.

Reverse a String with STRSPLIT

The first method to reverse a string uses the strsplit() function. These are the steps:

  1. Firstly, use the strsplit() function to create a vector where each vector element is a character of the original string.
  2. Secondly, reverse the elements of the vector with the rev() function.
  3. Finally, combine the reversed elements with the paste() function into a new string.

The trick of this method is to create a vector whose elements are the characters of the string. You can do this with the strsplit() function. You need to set the second argument of this function to NULL to convert each character into a separate element.

In the R code below, we show an example of how to reverse the characters of a string with the strsplit() function.

string_split <- strsplit("summer", NULL)[[1]]
string_split

reversed_string <- paste(rev(string_split), collapse="")
reversed_string
Reverse a String with STRSPLIT

Reverse a String with utf8ToInt

The second method to reverse the characters of a string in R uses the intToUtf8() function and the utf8ToInt() function.

Reversing a string with the utf8ToInt() function consists of 3 steps:

  1. Firstly, use the utf8ToInt() function to convert each character of a string into its corresponding integer.
  2. Secondly, invert the order of the vector with integers with the rev() function.
  3. Finally, use the intToUtf8() function to translate each integer into its equivalent character.

Below we show the results of each step

char_to_int <- utf8ToInt("summer")
char_to_int

rev_char_to_int <- rev(char_to_int)
rev_char_to_int

int_to_char <- intToUtf8(rev_char_to_int)
int_to_char

You can also combine the steps into one single line of code.

reversed_string <- intToUtf8(rev(utf8ToInt("summer")))
reversed_string

Reverse a String with STRINGI

The third method to reverse a string in R uses the stringi package.

The stringi package offers a variety of character string processing tools, one of which is the stri_reverse() function.

You can use the stri_reverse() function to reverse the characters of a string in R. The function has one argument, namely, the character string. By running this function, it returns a string with the characters in reversed order.

Below we show how to use the stri_reverse() function to reverse the characters of the word summer. Please make sure that you’ve installed and loaded the stringi package.

library(stringi)
reversed_string <- stri_reverse("summer")
reversed_string
Reverse a String with STRINGI

What is the Fastest Method to Reverse a String in R?

Above we have discussed 3 ways to reverse a string in R.

In general, your strings won’t be very long. However, if you have long(er) strings, the method you choose impacts (significantly) the time it takes to reverse them.

Below we show a comparison of the time it took to reverse four strings of different lengths with the three methods. We compared reversing strings with a length of 10, 100, 1.000, and 10.000 characters.

Method# of Characters per StringExecution Time
strsplit()104.8e-05
strsplit() 1006.4e-05
strsplit() 1.0006.8e-04
strsplit() 10.0005.3e-03
utf8ToInt()103.9e-05
utf8ToInt() 1002.1e-05
utf8ToInt() 1.0001.4e-04
utf8ToInt() 10.0004.7e-04
stri_reverse()101.2e-05
stri_reverse() 1001.1e-05
stri_reverse() 1.0003.3e-05
stri_reverse() 10.0001.1e-04

As the table and graph show, the stri_reverse() function is the fastest method when it comes to reversing a string in R.

For shorter strings, the performance of the three methods is similar. But, for longer strings (i.e., more than 1.000 characters) the stri_reverse() method is clearly the fastest.

How to Reverse the Words of a String in R

So far, we’ve shown 3 ways to reverse the characters in a string. In this section, we discuss one method to reverse the words in a string in R.

More specifically, we want to reverse the words “it is summer“. The desired result is “summer is it“.

You can reverse the words of a string in R with 3 simple steps:

  1. Firstly, use the strsplit() function to create a vector of the string where each vector element corresponds to one word.
  2. Secondly, use the rev() function to reverse the elements of the vector.
  3. Finally, use the paste() function to combine the elements of the vector into a string.

The example code below shows how to combine these 3 steps.

string_split <- strsplit("it is summer", " ")[[1]]
string_split

reversed_string <- paste(rev(string_split), collapse=" ")
reversed_string
How to Reverse the Words of a String in R

Related Articles