0
0
R Programmingprogramming~20 mins

Numeric and character vectors in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vector Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combining numeric and character vectors
What is the output of this R code when combining numeric and character vectors?
R Programming
v1 <- c(1, 2, 3)
v2 <- c("a", "b", "c")
v3 <- c(v1, v2)
print(v3)
A[1] "1" "2" "3" "a" "b" "c"
B[1] 1 2 3 "a" "b" "c"
C[1] 1 2 3 NA NA NA
D[1] 1 2 3 4 5 6
Attempts:
2 left
💡 Hint
Remember that combining numeric and character vectors converts all elements to characters.
Predict Output
intermediate
1:30remaining
Length of a mixed vector
What is the length of the vector created by this code?
R Programming
v1 <- c(10, 20, 30)
v2 <- c("x", "y")
v3 <- c(v1, v2)
length(v3)
ANA
B3
C2
D5
Attempts:
2 left
💡 Hint
The length counts all elements after combining.
🔧 Debug
advanced
2:00remaining
Error in vector creation with mixed types
What error does this R code produce?
R Programming
v <- c(1, "two", TRUE, 4.5)
print(v)
ANo error; prints a character vector: "1" "two" "TRUE" "4.5"
BError: unexpected string constant
CError: cannot combine numeric and logical values
DNo error; prints a numeric vector: 1 0 1 4.5
Attempts:
2 left
💡 Hint
Check how R coerces different types in a vector.
🧠 Conceptual
advanced
1:30remaining
Understanding vector coercion priority
Which type will the vector have after combining these elements: numeric, logical, and character?
ANumeric
BCharacter
CLogical
DFactor
Attempts:
2 left
💡 Hint
R coerces to the most flexible type to hold all values.
Predict Output
expert
2:00remaining
Output of vector recycling in arithmetic
What is the output of this R code?
R Programming
v1 <- c(1, 2, 3, 4)
v2 <- c(10, 20)
v3 <- v1 + v2
print(v3)
AError: vectors must be the same length
B[1] 11 22 31 42
C[1] 11 22 13 24
D[1] 11 22 11 22
Attempts:
2 left
💡 Hint
Remember how R recycles shorter vectors in arithmetic operations.