vendredi 14 septembre 2018

gsub function using if statements

I have a gsub function that replaces text within a column which works well. However, I now face an issue where I wish to replace certain text within the column but only for specific conditions, otherwise I wish to leave it as is.

The existing code looks like this:

myfulldata$Waypoint <- gsub("Old", "New", myfulldata$Waypoint, fixed = TRUE)

I now wish to have an if statement where I only replace the old text with the new text if the Sales.Year value = 2018 and Sales.Month=1 or 2. Can an if statement be nested within the formula?

I have looked at the exception case within the gsub formula but this seemed to only be for specific text within the column being replaced.

Aucun commentaire:

Enregistrer un commentaire