Bird
Raised Fist0
R Programmingprogramming~20 mins

Vector creation with c() in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vector Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of combining different types with c()
What is the output of this R code?
x <- c(1, TRUE, "3", 4.5)
print(x)
R Programming
x <- c(1, TRUE, "3", 4.5)
print(x)
A[1] 1 1 3 4.5
B[1] "1" "TRUE" "3" "4.5"
C[1] "1" "1" "3" "4.5"
D[1] 1 TRUE "3" 4.5
Attempts:
2 left
💡 Hint
Remember that c() converts all elements to the most flexible type to keep them in one vector.
❓ Predict Output
intermediate
1:30remaining
Length of vector created with c()
What is the length of the vector created by this code?
v <- c(NA, NULL, 5, c(2, 3))
length(v)
R Programming
v <- c(NA, NULL, 5, c(2, 3))
length(v)
A5
B2
C3
D4
Attempts:
2 left
💡 Hint
Remember that NULL is ignored inside c(), but NA counts as an element.
🔧 Debug
advanced
1:30remaining
Why does this c() call produce an error?
This code produces an error. What is the cause?
v <- c(1, 2, 3, , 5)
R Programming
v <- c(1, 2, 3, , 5)
ASyntax error due to missing value between commas
BNo error, code runs fine
CRuntime error because c() cannot handle more than 4 elements
DType error because 5 is not compatible with previous numbers
Attempts:
2 left
💡 Hint
Check the commas carefully inside the c() function.
🧠 Conceptual
advanced
1:30remaining
Behavior of c() with named elements
What is the output of this R code?
v <- c(a=1, b=2, 3)
names(v)
R Programming
v <- c(a=1, b=2, 3)
names(v)
A[1] "a" "b" "3"
B[1] "a" "b" "c"
C[1] "a" "b" ""
D[1] NULL
Attempts:
2 left
💡 Hint
Only elements explicitly named get names; unnamed elements have empty names.
❓ Predict Output
expert
2:00remaining
Output of nested c() calls with mixed types
What is the output of this R code?
v <- c(1, c(TRUE, "text"), 3.5)
print(v)
R Programming
v <- c(1, c(TRUE, "text"), 3.5)
print(v)
A[1] "1" "TRUE" "text" "3.5"
B[1] 1 1 "text" 3.5
C[1] "1" "1" "text" "3.5"
D[1] 1 TRUE "text" 3.5
Attempts:
2 left
💡 Hint
Remember that c() flattens nested vectors and coerces types to the most flexible one.

Practice

(1/5)
1. What does the c() function do in R?
easy
A. Reads data from a file
B. Creates a matrix
C. Combines values into a vector
D. Deletes variables

Solution

  1. Step 1: Understand the purpose of c()

    The c() function is used to combine multiple values into a single vector in R.
  2. Step 2: Compare with other options

    Creating a matrix, deleting variables, or reading files are done by other functions, not c().
  3. Final Answer:

    Combines values into a vector -> Option C
  4. Quick Check:

    c() = combine values [OK]
Hint: Remember: c() means combine values into one vector [OK]
Common Mistakes:
  • Thinking c() creates matrices
  • Confusing c() with file reading functions
  • Assuming c() deletes variables
2. Which of the following is the correct syntax to create a numeric vector with values 1, 2, and 3 in R?
easy
A. vector(1, 2, 3)
B. c(1, 2, 3)
C. c[1, 2, 3]
D. combine(1, 2, 3)

Solution

  1. Step 1: Recall the syntax for vector creation

    In R, c() is used to combine values into a vector, so c(1, 2, 3) is correct.
  2. Step 2: Identify incorrect syntax

    vector() is a different function needing length/type, c[] is invalid syntax, and combine() is not an R function.
  3. Final Answer:

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

    Correct vector syntax = c(1, 2, 3) [OK]
Hint: Use parentheses with c(), not brackets or other words [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses ()
  • Using vector() without proper arguments
  • Trying non-existent functions like combine()
3. What is the output of the following R code?
vec <- c(5, 10, 15)
print(vec[2])
medium
A. 10
B. 5
C. 15
D. Error

Solution

  1. Step 1: Understand vector indexing in R

    R vectors are 1-indexed, so vec[2] accesses the second element.
  2. Step 2: Identify the second element in the vector

    The vector is c(5, 10, 15), so the second element is 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Second element of c(5,10,15) = 10 [OK]
Hint: Remember R starts counting at 1, not 0 [OK]
Common Mistakes:
  • Assuming indexing starts at 0
  • Confusing element positions
  • Expecting an error from valid indexing
4. Identify the error in this R code:
my_vec <- c(1, 2, 3
print(my_vec)
medium
A. Missing closing parenthesis in c()
B. Using print() incorrectly
C. Vector cannot contain numbers
D. Variable name is invalid

Solution

  1. Step 1: Check the syntax of c()

    The c() function call is missing a closing parenthesis after the numbers.
  2. Step 2: Verify other parts of the code

    The print() function and variable name are correct, and vectors can contain numbers.
  3. Final Answer:

    Missing closing parenthesis in c() -> Option A
  4. Quick Check:

    Syntax error due to missing ')' [OK]
Hint: Count your parentheses carefully [OK]
Common Mistakes:
  • Forgetting to close parentheses
  • Thinking print() is wrong
  • Believing vectors can't hold numbers
5. You want to create a vector of mixed types: numbers and text, using c(). Which code correctly creates a vector with 10, "apple", and TRUE?
hard
A. c(10 + "apple" + TRUE)
B. c(10; "apple"; TRUE)
C. c(10 "apple" TRUE)
D. c(10, "apple", TRUE)

Solution

  1. Step 1: Understand how c() handles mixed types

    The c() function can combine numbers, text, and logical values into a vector, coercing to a common type if needed.
  2. Step 2: Check syntax correctness

    c(10, "apple", TRUE) uses commas to separate values correctly. Options A, B, and D use invalid separators or operators.
  3. Final Answer:

    c(10, "apple", TRUE) -> Option D
  4. Quick Check:

    Correct mixed vector syntax uses commas [OK]
Hint: Separate items with commas inside c() [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Trying to add different types with +
  • Omitting commas between values
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