Replace whole rows based on partial match

New to R. Looking to replace the entire string if there is a partial match.

d = c("SDS0G2 Blue", "Blue SSC2CWA3", "Blue SA2M1GC", "SA5 Blue CSQ5") gsub("Blue", "Red", d, ignore.case = FALSE, fixed = FALSE) 

Output: "SDS0G2 Red" "Red SSC2CWA3" "Red SA2M1GC" "SA5 Red CSQ5"

Desired output: "Red" "Red" "Red" "Red"

Any help in resolving this is truly appreciated.

+6
source share
1 answer

I would suggest using grepl to find indexes and replace these indexes with "Red":

 d = c("SDS0G2 Blue", "Blue SSC2CWA3", "Blue SA2M1GC", "SA5 Blue CSQ5", "ABCDE") d[grepl("Blue", d, ignore.case=FALSE)] <- "Red" d # [1] "Red" "Red" "Red" "Red" "ABCDE" 
+4
source

All Articles