Replace Last Comma in Character with &-Sign in R
Last Updated :
02 Aug, 2022
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
str <- "Hi Neena, Reena, Teena"
print ( "Input String" )
print (str)
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
library ( "stringr" )
str <- "Hi A, B, C"
print ( "Input String" )
print (str)
loc_comma <- str_locate_all (str, "," )[[1]]
comma_ele <- nrow (loc_comma)
last_loc_comma <- loc_comma[ comma_ele , ]
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
library ( "stringi" )
str <- "Hi A, B, C"
print ( "Input String" )
print (str)
out_str <- stri_replace_last (
str, fixed = "," , " &" )
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
str <- "Hi A, B, C"
print ( "Input String" )
print (str)
splt_str <- strsplit (str, "" )
str_unlist <- unlist (splt_str)
str_comma <- grep ( "," , str_unlist)
str_unlist[ tail (str_comma, 1)] <- " &"
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"
Share your thoughts in the comments
Please Login to comment...