Question: what is the difference between sub and gsub in r?
Answer: The difference is that sub only replaces the first occurrence of the pattern specified, whereas gsub does it for all occurrences (that is, it replaces globally). sub and gsub perform replacement of the first and all matches respectively.