R Replace Last Comma in Character with &-Sign (5 Examples)

If you are working with text data, you will often have a list or enumeration of words that is separated by commas, e.g.:

“Ball, Animal, Beer, Pig, Wagon, Balloon, Hero”

The last comma of such enumerations should usually be replaced by the word and or by the & sign. However, in R this might be challenging, since it might be difficult to grab only the last comma of your string.

In the following tutorial, I’m therefore going to show you in five examples how to replace the last comma of a character with the & sign in the R programming language.

Note: The original code of these examples comes from other people that replied to a question that I recently asked at the Stack Overflow forum. The thread was getting popular quickly and for that reason I decided to create a tutorial on this topic.

Let’s dive in…

 

Example 1: Replace Last Comma in R (sub Function)

Before we can start with the first example, I’m going to create an example character string which I’m going to use for all examples during this tutorial:

x <- "word1, word2, word3"                          # Create example character in R
x                                                   # Example character output in R
# "word1, word2, word3"

Our example character contains three words separated by commas.

The base installation of R provides the sub function, which can be used to replace the last comma with the & sign as follows:

x1 <- sub(",([^,]*)$", " &\\1", x)                  # Apply sub function
x1                                                  # Output of sub function
# "word1, word2 & word3"

Exactly as we wanted! However, the R syntax of the sub function can be inconvenient (you need some practice to master codes like this: “,([^,]*)$”, ” &\\1″, x).

For that reason, I’m going to show you an even easier solution for the replacement of the last comma of a character string in the next example…

 

Example 2: Replace Last Comma via stringi R Package (stri_replace_last Function)

For the second example, we need to install and load the stringi R add-on package:

install.packages("stringi")                         # Install stringi R package
library("stringi")                                  # Load stringi R package

The package contains the stri_replace_last R function, which provides an easy syntax for the substitution of the last comma of our character:

x2 <- stri_replace_last(x, fixed = ",", " &")       # Apply stri_replace_last
x2                                                  # Output of stri_replace_last
# "word1, word2 & word3"

Same output as before, but much easier to understand (if you ask me).

However, there are even more possibilities how to replace the last comma of a character…

 

Example 3: stringr R Package (str_locate_all & str_sub Functions)

Another package that can be used for the replacement of commas is the stringr Package.

Let’s install and load the package:

install.packages("stringr")                         # Install stringr R package
library("stringr")                                  # Load stringr R package

We can use the package to replace the last comma of our character in two steps. First, we need to apply the str_locate_all function in order to create a matrix that contains the locations of all commas in our character object:

x3 <- x                                             # Replicate example character
comma_pos <- str_locate_all(x3, ",")[[1]]           # Save all positions of commas
comma_pos                                           # Output of str_locate_all

 

Matrix with All Comma Positions

Table 1: Matrix Consisting of All Comma Positions.

 

In Table 1, you can see how the output matrix of str_locate_all looks like.

Now, we can use the str_sub command to replace the position of the last comma (stored in the previously created matrix) with the & sign:

comma_pos_last <- comma_pos[nrow(comma_pos), ]      # Save position of last comma
str_sub(x3, pos[1], pos[2]) <- " &"                 # Replace last comma with & sign
x3                                                  # Output of str_sub function
# "word1, word2 & word3"

Works! But stay with me. There is even one more example how to replace a comma in a string…

 

Example 4: Substitute Comma in 3 Steps (strsplit, grep & paste0 Functions)

For the fourth example, we will again rely on the base installation of R (as we did in Example 1). The example consists of three major steps.

Step 1) Use strsplit and unlist to get a vector of single characters:

x_split <- unlist(strsplit(x, ""))                  # Split character with strsplit
x_split                                             # Output of strsplit function
# "w" "o" "r" "d" "1" "," " " "w" "o" "r" "d" "2" "," " " "w" "o" "r" "d" "3"

Step 2) Use tail and grep to replace the last comma:

x_split[tail(grep(",", x_split), 1)] <- " &"        # Replace last comma with &
x_split
# "w" "o" "r" "d" "1" "," " " "w" "o" "r" "d" "2" "&" " " "w" "o" "r" "d" "3"

Step 3) Use past0 to merge all characters into one data object:

x4 <- paste0(x_split, collapse = "")                # Apply paste0 R function
x4                                                  # Output of paste0
# "word1, word2 & word3"

Same result, but relatively complicated.

 

Example 5: Replace Comma with Other Values or Remove Entirely

So far, we have only replaced the last comma with the & sign, but of cause we can also use other values for the replacement instead. In the following, I have prepared a few more examples, all based on the R code that I have shown in Example 1 (i.e. the sub function).

We can, for instance, replace the last comma with the word and…

sub(",([^,]*)$", " and\\1", x)                      # Replace last comma with and
# "word1, word2 and word3"

…with a dot (i.e. “.”)…

sub(",([^,]*)$", ".\\1", x)                         # Replace last comma with .
# "word1, word2. word3"

…with a semi-colon (i.e. “;”)…

sub(",([^,]*)$", ";\\1", x)                         # Replace last comma with ;
# "word1, word2; word3"

…with different text…

sub(",([^,]*)$", " ---any other text you want---\\1", x)    # Replace with text
# "word1, word2 ---any other text you want--- word3"

…or we can remove the last comma entirely:

sub(",([^,]*)$", "\\1", x)                          # Remove last comma entirely
# "word1, word2 word3"

 

Video: Dealing with Characters in R

In this tutorial I have shown you how to replace the last comma of a character string in R. However, the basement of all these functions and commands is the understanding of the character class.

If you want to learn more about the manipulation of characters, I can recommend the following video of Steve Pittard’s YouTube channel. In the video, he is explaining the difference of character vectors vs. character strings.

Have fun with the video and let me know in the comments in case you have further questions or in case you have any kind of feedback.

 

 

Further Reading

 



 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu