0
0
R Programmingprogramming~10 mins

Numeric and character vectors in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Numeric and character vectors
Start
Create vector
Check type: numeric or character?
Store numbers
Use vector
We start by creating a vector, then check if it holds numbers or text, and store accordingly for use.
Execution Sample
R Programming
nums <- c(10, 20, 30)
chars <- c("a", "b", "c")
print(nums)
print(chars)
This code creates a numeric vector and a character vector, then prints both.
Execution Table
StepActionVariableValueOutput
1Create numeric vectornumsc(10, 20, 30)
2Create character vectorcharsc("a", "b", "c")
3Print numeric vectornumsc(10, 20, 30)[1] 10 20 30
4Print character vectorcharsc("a", "b", "c")[1] "a" "b" "c"
5End of codeExecution complete
💡 All vectors created and printed successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
numsNULLc(10, 20, 30)c(10, 20, 30)c(10, 20, 30)
charsNULLNULLc("a", "b", "c")c("a", "b", "c")
Key Moments - 2 Insights
Why does nums hold numbers but chars hold letters?
Because nums was created with numbers inside c(), so R treats it as numeric vector; chars has text in quotes, so R treats it as character vector (see steps 1 and 2 in execution_table).
What happens if you mix numbers and letters in one vector?
R converts all elements to character type to keep vector consistent. This is because vectors must hold one type only.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of nums?
Ac("10", "20", "30")
Bc(10, 20, 30)
CNULL
Dc("a", "b", "c")
💡 Hint
Check the 'Value' column for nums at step 3 in execution_table.
At which step is the character vector chars created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when chars is assigned.
If you add a number to chars vector, what type will the vector become?
ANumeric
BLogical
CCharacter
DList
💡 Hint
Recall key moment about mixing types in vectors; R converts all to character.
Concept Snapshot
Numeric and character vectors in R hold numbers or text.
Use c() to create vectors: nums <- c(1,2,3), chars <- c("a","b","c").
Vectors hold one type only; mixing types converts all to character.
Print vectors with print() or just variable name.
Vectors are basic building blocks for data in R.
Full Transcript
This lesson shows how to create numeric and character vectors in R. We start by making a numeric vector named nums with numbers 10, 20, 30 using c(). Then we create a character vector named chars with letters "a", "b", "c" also using c(). We print both vectors to see their contents. The execution table traces each step: creating nums, creating chars, printing nums, printing chars, and finishing. The variable tracker shows how nums and chars change from NULL to their final vector values. Key moments clarify that nums holds numbers because it was created with numbers, chars holds letters because of quotes, and mixing types in one vector converts all to character. The quiz tests understanding of vector values at steps, creation steps, and type conversion rules. The snapshot summarizes how to create and use numeric and character vectors in R simply and clearly.