Bird
Raised Fist0
R Programmingprogramming~5 mins

Modifying and adding elements in R Programming - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
How do you change the value of an element in a vector in R?
You assign a new value to the element using its index. For example, vec[2] <- 10 changes the second element to 10.
Click to reveal answer
beginner
What function can you use to add elements to the end of a vector?
You can use the c() function to combine vectors or add elements. For example, vec <- c(vec, 5) adds 5 to the end.
Click to reveal answer
beginner
How do you add a new column to a data frame in R?
Assign a vector to a new column name. For example, df$new_col <- c(1,2,3) adds a column named 'new_col'.
Click to reveal answer
intermediate
How can you modify an element inside a list in R?
Use double brackets with the element name or index. For example, mylist[["item"]] <- 42 changes the 'item' element.
Click to reveal answer
intermediate
What happens if you assign a value to an index outside the current length of a vector?
R extends the vector and fills missing elements with NA. For example, vec[5] <- 10 makes elements 3 and 4 NA if they didn't exist.
Click to reveal answer
Which syntax correctly adds a new element 7 to the end of vector v?
Av <- c(v, 7)
Bv[7] <- v
Cv + 7
Dappend(v, 7)
How do you change the third element of vector x to 100?
Ax(3) = 100
Bx[3] <- 100
Cx <- 100[3]
Dx[[3]] <- 100
Which is the correct way to add a new column named 'age' to data frame df?
Adf <- c(df, age)
Bdf[age] <- c(25, 30, 35)
Cdf(age) <- c(25, 30, 35)
Ddf$age <- c(25, 30, 35)
What happens if you assign a value to vec[10] when vec has length 5?
AVector extends; elements 6 to 9 become NA
BError: index out of bounds
CNothing changes
DVector shrinks to length 10
How do you modify the element named 'score' in a list lst?
Alst[score] <- new_value
Blst.score <- new_value
Clst[['score']] <- new_value
Dlst(score) <- new_value
Explain how to modify an element in a vector and add a new element at the end in R.
Think about using brackets for modification and c() for adding.
You got /3 concepts.
    Describe how to add a new column to a data frame and how to modify an element inside a list.
    Remember data frames and lists have different ways to access elements.
    You got /3 concepts.

      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