Vasista B
Vasista B

Reputation: 339

gsub in R with case condition in R

I have data like : PES + PEA + PWH

I want to use gsub or any other function in R such that -- if there is a PEA in the data then keep PEA (and PWH should be kept too) and remove just PES

So ideally the condition should involve only PEA and PES.

Final Output : PEA + + PWH Thanks!

Upvotes: 2

Views: 995

Answers (1)

Konrad
Konrad

Reputation: 18585

The question would be easier to answer if you could provide a sample data set. Assuming that your intention is to replace certain strings taking into account capitals the ignore.case = FALSE should be enough. For example:

x <- "PES + PEA + PWH + pes"
gsub("PES","New text",x, ignore.case = FALSE)

would give you:

[1] "New text + PEA + PWH + pes"

Upvotes: 4

Related Questions