Bird
Raised Fist0
R Programmingprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly creates a numeric vector in R?
easy
A. c(TRUE, FALSE, TRUE)
B. c("one", "two", "three")
C. c(1, "two", 3)
D. c(1, 2, 3, 4)

Solution

  1. Step 1: Understand numeric vector creation

    Numeric vectors contain only numbers, so elements must be numeric values.
  2. Step 2: Check each option's elements

    c(1, 2, 3, 4) has only numbers, so it is a numeric vector. c("one", "two", "three") has text, c(TRUE, FALSE, TRUE) has logical values, and c(1, "two", 3) mixes numbers and text, which converts all to characters.
  3. Final Answer:

    c(1, 2, 3, 4) -> Option D
  4. Quick Check:

    Numeric vector = c(1, 2, 3, 4) [OK]
Hint: Numeric vectors contain only numbers inside c() [OK]
Common Mistakes:
  • Mixing numbers and text creates character vector
  • Confusing logical vectors with numeric
  • Using quotes around numbers
2. Which of these is the correct syntax to create a character vector in R?
easy
A. c(1, 2, 3)
B. c("apple", "banana", "cherry")
C. c(TRUE, FALSE, TRUE)
D. c(1, "banana", 3)

Solution

  1. Step 1: Identify character vector syntax

    Character vectors contain text strings, which must be inside quotes.
  2. Step 2: Check options for quoted text

    c("apple", "banana", "cherry") has all elements as quoted text strings, so it is a character vector. c(1, 2, 3) is numeric, c(TRUE, FALSE, TRUE) is logical, and c(1, "banana", 3) mixes types causing coercion.
  3. Final Answer:

    c("apple", "banana", "cherry") -> Option B
  4. Quick Check:

    Character vector = c("apple", "banana", "cherry") [OK]
Hint: Use quotes around text to create character vectors [OK]
Common Mistakes:
  • Forgetting quotes around text
  • Mixing types in one vector
  • Using numbers without quotes for text
3. What is the output of this R code?
x <- c(5, 10, 15)
y <- c("a", "b", "c")
z <- c(x, y)
print(z)
medium
A. [1] "5" "10" "15" "a" "b" "c"
B. [1] 5 10 15 "a" "b" "c"
C. [1] 5 10 15 NA NA NA
D. Error: cannot combine numeric and character vectors

Solution

  1. Step 1: Understand vector combining rules in R

    When combining numeric and character vectors, R converts all elements to character to keep the same type.
  2. Step 2: Analyze the combined vector z

    Vector z contains numbers 5, 10, 15 and characters "a", "b", "c". All numbers become strings, so print shows all as character strings.
  3. Final Answer:

    [1] "5" "10" "15" "a" "b" "c" -> Option A
  4. Quick Check:

    Mixed vector coerces to character = [1] "5" "10" "15" "a" "b" "c" [OK]
Hint: Mixed types in vector become character strings [OK]
Common Mistakes:
  • Expecting numeric values to stay numeric
  • Thinking R throws error on combining types
  • Assuming NA values appear for mismatched types
4. Identify the error in this R code:
vec <- c(1, 2, "three", 4)
sum(vec)
medium
A. No error; sum returns 10
B. sum() returns NA due to mixed types
C. Error because sum() cannot handle character vectors
D. Error because c() cannot mix numbers and text

Solution

  1. Step 1: Understand vector creation with mixed types

    c(1, 2, "three", 4) creates a character vector because of the text element "three".
  2. Step 2: Check sum() behavior on character vectors

    sum() expects numeric input. Passing a character vector causes an error because it cannot sum text.
  3. Final Answer:

    Error because sum() cannot handle character vectors -> Option C
  4. Quick Check:

    sum(character vector) = Error [OK]
Hint: sum() needs numeric vectors, not characters [OK]
Common Mistakes:
  • Assuming sum() converts characters to numbers
  • Thinking c() forbids mixing types (it coerces instead)
  • Expecting sum() to return NA silently
5. You have two vectors:
nums <- c(2, 4, 6, 8)
chars <- c("2", "4", "6", "8")

How can you create a numeric vector from chars and add it to nums element-wise?
hard
A. nums + as.numeric(chars)
B. nums + chars
C. as.character(nums) + chars
D. sum(nums, chars)

Solution

  1. Step 1: Convert character vector to numeric

    Use as.numeric(chars) to change text numbers to numeric values.
  2. Step 2: Add numeric vectors element-wise

    Adding nums + as.numeric(chars) adds corresponding elements correctly.
  3. Final Answer:

    nums + as.numeric(chars) -> Option A
  4. Quick Check:

    Convert chars to numeric before adding = nums + as.numeric(chars) [OK]
Hint: Convert text to numbers with as.numeric() before math [OK]
Common Mistakes:
  • Adding numeric and character vectors directly
  • Using sum() instead of element-wise addition
  • Converting numeric to character instead of chars to numeric
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard