0
0
R Programmingprogramming~10 mins

sub and gsub in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sub and gsub
Input String
Apply Pattern Match
sub: Replace first match only
Output String
gsub: Replace all matches
Output String
The flow shows how sub replaces only the first pattern match in a string, while gsub replaces all matches.
Execution Sample
R Programming
text <- "apple apple apple"
sub("apple", "orange", text)
gsub("apple", "orange", text)
Replace 'apple' with 'orange' once using sub, and all occurrences using gsub.
Execution Table
StepFunctionInput StringPatternReplacementResult
1sub"apple apple apple""apple""orange""orange apple apple"
2gsub"apple apple apple""apple""orange""orange orange orange"
💡 Both functions finish after replacing matches; sub replaces only the first, gsub replaces all.
Variable Tracker
VariableStartAfter subAfter gsub
text"apple apple apple""apple apple apple""apple apple apple"
result_subNA"orange apple apple"NA
result_gsubNANA"orange orange orange"
Key Moments - 2 Insights
Why does sub only replace the first 'apple' and not the others?
sub stops after the first match and replaces it, as shown in execution_table row 1 where only the first 'apple' changes.
Does gsub change the original string variable?
No, gsub returns a new string with all replacements; the original 'text' variable remains unchanged as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of sub at step 1?
A"orange apple apple"
B"apple orange apple"
C"orange orange orange"
D"apple apple apple"
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the function replace all occurrences of 'apple'?
AStep 1
BStep 2
CNeither step
DBoth steps
💡 Hint
Look at the 'Function' and 'Result' columns in execution_table.
If we assign the result of gsub to 'text', what will variable_tracker show after gsub?
A"apple apple apple"
B"orange apple apple"
C"orange orange orange"
DNA
💡 Hint
Refer to variable_tracker row for 'result_gsub' and imagine assigning it back to 'text'.
Concept Snapshot
sub(pattern, replacement, x) replaces only the first match in x.
gsub(pattern, replacement, x) replaces all matches in x.
Both return a new string; original string stays unchanged unless reassigned.
Use sub for single replacement, gsub for global replacement.
Full Transcript
In R, sub and gsub are functions to replace text patterns in strings. sub replaces only the first occurrence of the pattern, while gsub replaces all occurrences. For example, given the string 'apple apple apple', sub("apple", "orange", text) changes only the first 'apple' to 'orange', resulting in 'orange apple apple'. gsub("apple", "orange", text) changes all 'apple' words to 'orange', resulting in 'orange orange orange'. Neither function changes the original string unless you assign the result back to the variable. This visual trace shows each step and how variables change, helping beginners understand the difference clearly.