- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace total string if partial string matches with another string in R data frame column?
To replace total string if partial string matches with another string in R data frame column, we can follow the below steps −
First of all, create a data frame with string column.
Then, use gsub function to replace total string if partial string matches with another string.
Example
Create the data frame
Let’s create a data frame as shown below −
String<-sample(c("india1234","china1234","russia3541","uk3541","south africa3541","nepal1234","sri lanka1234","canada3541","sudan1234","korea1234","belarus3541"),25,replace=TRUE) df<-data.frame(String) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
String 1 south
africa3541 2 russia3541 3 sudan1234 4 india1234 5 south
africa3541 6 india1234 7 south
africa3541 8 korea1234 9 korea1234 10 uk3541 11 india1234 12 korea1234 13 belarus3541 14 belarus3541 15 korea1234 16 china1234 17 sudan1234 18 uk3541 19 india1234 20 korea1234 21 korea1234 22 sudan1234 23 uk3541 24 china1234 25 korea1234
Replace total string if partial string matches
Using gsub function to replace total string with No if 3541 exists in the string as shown below −
String<-sample(c("india1234","china1234","russia3541","uk3541","south africa3541","nepal1234","sri lanka1234","canada3541","sudan1234","korea1234","belarus3541"),25,replace=TRUE) df<-data.frame(String) df$Replaced_S<-gsub(".*3541.*","No",df$String) df
Output
String Replaced_S 1 china1234 china1234 2 korea1234 korea1234 3 russia3541 No 4 sri
lanka1234 sri
lanka1234 5 russia3541 No 6 nepal1234 nepal1234 7 korea1234 korea1234 8 russia3541 No 9 sudan1234 sudan1234 10 sudan1234 sudan1234 11 sudan1234 sudan1234 12 south
africa3541 No 13 nepal1234 nepal1234 14 nepal1234 nepal1234 15 south
africa3541 No 16 nepal1234 nepal1234 17 canada3541 No 18 sri
lanka1234 sri
lanka1234 19 china1234 china1234 20 sudan1234 sudan1234 21 south
africa3541 No 22 nepal1234 nepal1234 23 china1234 china1234 24 sri
lanka1234 sri
lanka1234 25 china1234 china1234
- Related Articles
- How to look for partial string matches in queries in PostgreSQL?
- How to replace space in a string value for some elements in a column of an R data frame?
- Replace String with another in java.
- How to remove single quote from string column in an R data frame?
- How to find the column number of a string column based on a string match in an R data frame?
- How to replace all occurrences of a string with another string in Python?
- Extract columns with a string in column name of an R data frame.
- Replace part of a string with another string in C++
- Replace one string with another string with Java Regular Expressions
- How to create a column with largest size string value in rows in an R data frame?
- How to extract number from string if string is in different format than normal in R data frame?
- How to extract number from string in R data frame?
- Check which column names contain a specific string in R data frame.
- How to add a string before each numeric value in an R data frame column?
- How to find the length of the largest string in an R data frame column?

Advertisements