Bird
Raised Fist0
R Programmingprogramming~20 mins

Modifying and adding elements in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
R Vector and List Modifier
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this R code modifying a vector element?
Consider the following R code that modifies an element in a vector. What will be printed?
R Programming
vec <- c(10, 20, 30, 40)
vec[2] <- 25
print(vec)
A[1] 10 20 30 40
B[1] 10 25 30 40
C[1] 25 20 30 40
D[1] 10 25 25 40
Attempts:
2 left
💡 Hint
Remember that indexing in R starts at 1 and you can assign a new value to a specific position.
❓ Predict Output
intermediate
2:00remaining
What happens when adding a new element to a list in R?
What will be the output of this R code that adds a new element to a list?
R Programming
my_list <- list(a=1, b=2)
my_list$c <- 3
print(my_list)
AError in my_list$c <- 3 : object is not a list
B[1] 1 2 3
C
$a
[1] 1

$b
[1] 2

$c
[1] 3
Dlist(a=1, b=2, c=3)
Attempts:
2 left
💡 Hint
In R, you can add named elements to a list using the $ operator.
🔧 Debug
advanced
2:00remaining
Why does this R code produce an error when modifying a vector element?
Identify the error in this R code and explain why it happens.
R Programming
vec <- c(1, 2, 3)
vec[4] <- 5
print(vec)
ANo error; output is [1] 1 2 3 5
BWarning: NAs introduced by coercion
CError: subscript out of bounds
DError: object 'vec' not found
Attempts:
2 left
💡 Hint
In R, vectors can be extended by assigning to an index beyond their current length.
❓ Predict Output
advanced
2:00remaining
What is the output after modifying a data frame column in R?
Given this R code that modifies a data frame column, what will be printed?
R Programming
df <- data.frame(x = 1:3, y = c('a', 'b', 'c'))
df$x[2] <- 10
print(df)
A
  x y
1  1 a
2 10 b
3  3 c
B
  x y
1  1 a
2  2 b
3  3 c
CError: replacement has length zero
D
  x y
1 10 a
2 10 b
3 10 c
Attempts:
2 left
💡 Hint
You can modify specific elements of a data frame column by indexing.
🧠 Conceptual
expert
2:00remaining
How does R handle adding elements beyond current vector length?
In R, what happens when you assign a value to an index beyond the current length of a vector?
AR truncates the vector to the assigned index and assigns the value
BR throws an error: subscript out of bounds
CR ignores the assignment silently without changing the vector
DR extends the vector, filling intermediate positions with NA, then assigns the value
Attempts:
2 left
💡 Hint
Think about what happens if you assign to an index that does not yet exist in the vector.

Practice

(1/5)
1. In R, how do you change the second element of a vector v to 10?
easy
A. v[2] = 10()
B. v(2) = 10
C. v{2} <- 10
D. v[2] <- 10

Solution

  1. Step 1: Understand element access in R vectors

    Elements in R vectors are accessed using square brackets [ ] with the position number inside.
  2. Step 2: Assign new value to the element

    Use the assignment operator <- to set the new value at the specified position.
  3. Final Answer:

    v[2] <- 10 -> Option D
  4. Quick Check:

    Use [ ] and <- to modify elements [OK]
Hint: Use square brackets and <- to change elements [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Adding parentheses after the value
  • Using curly braces for indexing
2. Which of the following is the correct syntax to add a new element 5 to the end of vector v in R?
easy
A. v + 5
B. v <- c(v, 5)
C. v[+5]
D. v <- v + c(5)

Solution

  1. Step 1: Understand how to add elements to a vector

    In R, use the c() function to combine vectors or add elements.
  2. Step 2: Combine existing vector with new element

    Use v <- c(v, 5) to add 5 at the end of vector v.
  3. Final Answer:

    v <- c(v, 5) -> Option B
  4. Quick Check:

    Use c() to add elements [OK]
Hint: Use c() to add elements to vectors [OK]
Common Mistakes:
  • Using + operator to add elements
  • Incorrect indexing syntax
  • Adding vectors without assignment
3. What will be the output of the following R code?
v <- c(1, 2, 3)
v[1] <- 10
v <- c(v, 4)
v
medium
A. [10, 2, 3]
B. [1, 2, 3, 4]
C. [10, 2, 3, 4]
D. [1, 2, 3]

Solution

  1. Step 1: Modify first element of vector

    The first line changes the first element from 1 to 10, so vector becomes (10, 2, 3).
  2. Step 2: Add new element 4 at the end

    Using c(v, 4) adds 4 to the end, resulting in (10, 2, 3, 4).
  3. Final Answer:

    [10, 2, 3, 4] -> Option C
  4. Quick Check:

    Modify then add element = [10, 2, 3, 4] [OK]
Hint: Modify first, then add with c() to get final vector [OK]
Common Mistakes:
  • Forgetting to assign after adding element
  • Not updating the first element correctly
  • Confusing vector output format
4. Identify the error in the following R code that tries to add 7 to vector v:
v <- c(1, 2, 3)
v + 7
medium
A. The code adds 7 to each element, not appends 7
B. Using + operator is correct to add elements
C. v + 7 appends 7 correctly to vector
D. Syntax error due to missing assignment operator

Solution

  1. Step 1: Understand vector addition vs adding elements

    Using v + 7 adds 7 to each element, but does not append a new element.
  2. Step 2: Check for assignment to update vector

    The code does not assign the result back to v, so vector remains unchanged. Also, to add an element, c() should be used.
  3. Final Answer:

    The code adds 7 to each element, not appends 7 -> Option A
  4. Quick Check:

    Adding elements needs c() and assignment [OK]
Hint: Use c() and <- to add elements, not + alone [OK]
Common Mistakes:
  • Using + to append elements
  • Not assigning result back to vector
  • Confusing element-wise addition with adding elements
5. Given a vector v <- c(2, 4, 6), which code correctly replaces the last element with 8 and then adds 10 at the end?
hard
A. v[length(v)] <- 8; v <- c(v, 10)
B. v[3] = 8; v + 10
C. v[-1] <- 8; v <- c(v, 10)
D. v[length(v)+1] <- 8; v <- c(v, 10)

Solution

  1. Step 1: Replace last element using length()

    Use v[length(v)] <- 8 to change the last element (position 3) to 8.
  2. Step 2: Add 10 at the end with c()

    Use v <- c(v, 10) to append 10 to the vector.
  3. Final Answer:

    v[length(v)] <- 8; v <- c(v, 10) -> Option A
  4. Quick Check:

    Use length() to index last element, c() to add [OK]
Hint: Use length(v) to find last element, c() to add new [OK]
Common Mistakes:
  • Using negative index incorrectly
  • Adding element with + operator
  • Assigning to length(v)+1 without c()
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