sub() function do in R?sub() replaces the first occurrence of a pattern in a string with a replacement.
gsub() different from sub()?gsub() replaces all occurrences of a pattern in a string, while sub() replaces only the first.
sub("a", "X", "banana")?The output is "bXnana" because only the first 'a' is replaced by 'X'.
gsub("a", "X", "banana")?The output is "bXnXnX" because all 'a's are replaced by 'X'.
sub() and gsub() work with regular expressions?Yes, both functions use regular expressions to find patterns in strings for replacement.
sub("cat", "dog", "catapult") return?sub() replaces the first occurrence of "cat" with "dog", so "catapult" becomes "dogapult".
gsub() replaces all occurrences of the pattern, unlike sub() which replaces only the first.
gsub("[aeiou]", "*", "hello world") output?All vowels (a, e, i, o, u) are replaced by '*', so "hello world" becomes "h*ll* w*rld".
sub() replaces only the first match, so it is used to replace the first space.
sub() and gsub()?Regular expressions define the pattern that sub() and gsub() look for to replace.
sub() and gsub() in R.gsub() instead of sub().