Difference between sub and gsub in R

sub function replaces the first match of a string, if the parameter is a string vector, replaces the first match of all elements.
gsub function replaces all matches of a string. g stands for global.
Syntax is same for both sub and gsub.

Examples:

> k< -"Welcome to k2schools and also visit to python tutorials" > sub("to","xyz",k) #Here, sub function replaces only first 'to' with 'xyz' [1] "Welcome xyz k2schools and also visit to python tutorials"

> k< -"Welcome to k2schools and also visit to python tutorials" > gsub("to","xyz",k) #Here gsub function replaces all 'to' with 'xyz' [1] "Welcome xyz k2schools and also visit xyz python tuxyzrials"

Related Posts

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools