0
0
R Programmingprogramming~10 mins

nchar and substring in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - nchar and substring
Start with a string
Use nchar to get length
Use substring(text, start, end) to extract part
Output substring
End
We start with a string, find its length using nchar, then extract a part using substring by specifying start and end positions.
Execution Sample
R Programming
text <- "Hello, World!"
len <- nchar(text)
part <- substring(text, 8, 12)
print(len)
print(part)
This code finds the length of the string and extracts characters from position 8 to 12.
Execution Table
StepVariableExpressionValueAction
1textAssign "Hello, World!""Hello, World!"Store string
2lennchar(text)13Calculate length of text
3partsubstring(text, 8, 12)"World"Extract substring from position 8 to 12
4print(len)print(13)13Output length
5print(part)print("World")WorldOutput substring
💡 All steps executed; program ends after printing substring.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
textundefined"Hello, World!""Hello, World!""Hello, World!""Hello, World!"
lenundefinedundefined131313
partundefinedundefinedundefined"World""World"
Key Moments - 2 Insights
Why does substring(text, 8, 12) return "World" and not include the comma or exclamation mark?
Because substring extracts characters from position 8 to 12 exactly. The comma is at position 6 and exclamation mark at 13, so they are outside this range (see execution_table step 3).
What happens if the end position in substring is greater than the string length?
R extracts characters up to the string's end without error. For example, substring(text, 8, 20) would return "World!" because the string ends at position 13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of len?
A13
B12
C11
D14
💡 Hint
Check the 'Value' column for step 2 in execution_table.
At which step is the substring variable assigned its value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where 'part' is assigned in execution_table.
If substring(text, 1, 5) was used instead, what would be the output at step 3?
A", Wor"
B"World"
C"Hello"
D"Hello,"
💡 Hint
Substring extracts characters from start to end positions; check positions 1 to 5 in the string.
Concept Snapshot
nchar(text) returns the number of characters in text.
substring(text, start, end) extracts characters from start to end positions.
Positions start at 1.
If end > length, substring returns up to string end.
Useful for slicing parts of strings easily.
Full Transcript
This example starts with a string variable text assigned "Hello, World!". The nchar function calculates its length, which is 13 characters. Then substring extracts characters from position 8 to 12, which is "World". The program prints the length and the extracted substring. Positions in substring start at 1, so the comma and exclamation mark are outside the extracted range. If the end position exceeds the string length, substring returns characters up to the end without error.