VOOZH about

URL: https://www.geeksforgeeks.org/r-language/replace-last-comma-in-character-with-sign-in-r/

⇱ Replace Last Comma in Character with &-Sign in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace Last Comma in Character with &-Sign in R

Last Updated : 23 Jul, 2025

A string in R is a sequence of characters which contains numbers, characters, and special symbols. The characters can be modified, inserted as well as deleted in the strings. R provides a large variety of functions and external packages to carry out the string amendments. In this article, we are going to see that how to Replace Last Comma in Character with &-Sign in R Programming Language.

Method 1 : Using sub() method

The sub() method in R is used to replace the specified part of the string with a new string. The usage of the sub() method is compatible with the regular expressions as well,. It has the following syntax : 

Syntax: sub(search, replacement, str)

Arguments : 

  • search - The search term to look into the string
  • replacement - The new string to replace the search term with
  • str - The string to carry out the replacement in

Output

[1] "Input String"
[1] "Hi Neena, Reena, Teena"
[1] "Output String"
[1] "Hi Neena, Reena & Teena"

Method 2: Using stringr package

The stringr package in R is used to carry out string manipulations. It can be downloaded and installed into the working space using the following command : 

install.packages("stringr")

A series of methods are contained within this package which can be used to carry out string modifications. Initially, the method str_locate_all() is applied to the string to locate all the occurrences of the comma string. 

Output

[1] "Input String"
[1] "Hi A, B, C"
[1] "Output String"
[1] "Hi A, B & C"

Method 3:  Using stringi package

The stringi package in R is used to carry out string manipulations. It can be downloaded and installed into the working space using the following command : 

install.packages("stringi")

The method stri_replace_last() is directly used to replace the last occurrence of the specified pattern in the string. The method has the following syntax : 

Syntax: stri_replace_last(str, fixed, replacement)

Arguments : 

  • str - The string to replace the character in 
  • fixed - The fixed pattern to look for
  • replacement - The string to replace the fixed pattern with

Here, in the last occurrence the character string "," 

Output

[1] "Input String"
[1] "Hi A, B, C"
[1] "Output String"
[1] "Hi A, B & C"

Method 4: Using in-built string methods

R contains a series of in-built methods to perform data amendments and accessing. Initially, the string is split into different segments using the strsplit() method, which is used to split the parts of the character vector given as an argument into the strsplit() method. It then divides it into different substrings with the specified substring. The method has the following syntax : 

Output

[1] "Input String"
[1] "Hi A, B, C"
[1] "Output String"
[1] "Hi A, B & C"
Comment
Article Tags:

Explore