Skip to content
Related Articles

Related Articles

Replace Last Comma in Character with &-Sign in R

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

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

R




# creating a dummy string 
str <- "Hi Neena, Reena, Teena"
print("Input String")
print(str)
  
# replace the last comma with &
out_str <- sub(",([^,]*)$", " &\\1", str) 
print("Output String")
print(out_str)

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. 

R




# installing the reqd libraries
library("stringr")
  
# creating a dummy string 
str <- "Hi A, B, C"
print("Input String")
print(str)
  
# locating the places of comma occurrence
loc_comma <- str_locate_all(str, ",")[[1]]  
  
# calculating the number of positions 
# in comma array
comma_ele <- nrow(loc_comma)
  
# fetching the last comma position
last_loc_comma <- loc_comma[ comma_ele , ] 
  
# replace the last comma with & symbol 
str_sub(str, last_loc_comma[1], 
        last_loc_comma[2]) <- " &"  
print("Output String")
print(str)

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 “,” 

R




# installing the reqd libraries
library("stringi")
  
# creating a dummy string 
str <- "Hi A, B, C"
print("Input String")
print(str)
  
# replacing the last comma with
# &
out_str <- stri_replace_last(
  str, fixed = ",", " &")  
  
# printing the output string
print("Output String")
print(out_str)

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 : 

R




# creating a dummy string 
str <- "Hi A, B, C"
print("Input String")
print(str)
  
# split the string characters
splt_str <- strsplit(str, "")
  
# divide the string into individual
# strings
str_unlist <- unlist(splt_str)   
  
# used to fetch the position of the 
# comma 
str_comma <- grep(",", str_unlist)
  
# replace the last position of
# the comma array with a & symbol 
str_unlist[tail(str_comma, 1)] <- " &" 
  
# remerge the string using empty spaces
out_str <- paste0(str_unlist, collapse = "")  
print("Output String")
print(out_str)

Output

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!