📖 Scenario: You are organizing a small party and want to keep track of the number of guests and their favorite drinks.
🎯 Goal: Create numeric and character vectors in R to store the number of guests and their favorite drinks, then display the information.
📋 What You'll Learn
Create a numeric vector called guest_counts with the exact values 5, 3, 7
Create a character vector called favorite_drinks with the exact values "Tea", "Coffee", "Juice"
Create a variable called total_guests that sums all values in guest_counts
Print the total_guests value
Print the favorite_drinks vector
💡 Why This Matters
🌍 Real World
Keeping track of guest counts and preferences helps in planning events and ordering the right amount of food and drinks.
💼 Career
Data analysts and event planners often work with numeric and character data to summarize and report information clearly.
Progress0 / 4 steps
1
Create the numeric vector
Create a numeric vector called guest_counts with the exact values 5, 3, 7.
R Programming
Hint
Use the c() function to combine numbers into a vector.
2
Create the character vector
Create a character vector called favorite_drinks with the exact values "Tea", "Coffee", "Juice".
R Programming
Hint
Use the c() function with quotes around each drink name.
3
Calculate total guests
Create a variable called total_guests that sums all values in the guest_counts vector.
R Programming
Hint
Use the sum() function to add all numbers in the vector.
4
Print the results
Print the value of total_guests and then print the favorite_drinks vector.
R Programming
Hint
Use print() to show the values on the screen.
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
Step 1: Understand numeric vector creation
Numeric vectors contain only numbers, so elements must be numeric values.
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.
Final Answer:
c(1, 2, 3, 4) -> Option D
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
Step 1: Identify character vector syntax
Character vectors contain text strings, which must be inside quotes.
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.
Final Answer:
c("apple", "banana", "cherry") -> Option B
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
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.
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.
Final Answer:
[1] "5" "10" "15" "a" "b" "c" -> Option A
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
Step 1: Understand vector creation with mixed types
c(1, 2, "three", 4) creates a character vector because of the text element "three".
Step 2: Check sum() behavior on character vectors
sum() expects numeric input. Passing a character vector causes an error because it cannot sum text.
Final Answer:
Error because sum() cannot handle character vectors -> Option C
Quick Check:
sum(character vector) = Error [OK]
Hint: sum() needs numeric vectors, not characters [OK]