Replace entire strings based on partial match

前端 未结 2 401
南旧
南旧 2020-12-06 11:45

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\")
         


        
2条回答
  •  春和景丽
    2020-12-06 12:29

    I'd suggest using grepl to find the indices and replace those indices 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"
    

提交回复
热议问题