Bird
Raised Fist0
R Programmingprogramming~20 mins

Adding and removing columns in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Column Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of adding a new column with cbind
What is the output of this R code after adding a new column using cbind?
R Programming
df <- data.frame(a = 1:3, b = 4:6)
df <- cbind(df, c = c(7, 8, 9))
print(df)
AError in cbind(df, c = c(7, 8, 9)) : object 'c' not found
B
  a b
1 1 4
2 2 5
3 3 6
c 7 8 9
C
  a b c
1 1 4 7
2 2 5 8
3 3 6 9
D
  a b c
1 7 8 9
2 1 4 7
3 2 5 8
Attempts:
2 left
💡 Hint
Remember that cbind adds columns side by side and keeps row order.
❓ Predict Output
intermediate
2:00remaining
Result of removing a column using NULL assignment
What will be the result of this R code after removing column 'b' by assigning NULL?
R Programming
df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
df$b <- NULL
print(df)
A
  a b
1 1 4
2 2 5
3 3 6
B
  a b c
1 1 4 7
2 2 5 8
3 3 6 9
CError: object 'b' not found
D
  a c
1 1 7
2 2 8
3 3 9
Attempts:
2 left
💡 Hint
Assigning NULL to a column removes it from the data frame.
🔧 Debug
advanced
2:00remaining
Why does this code fail to add a column?
This code tries to add a new column 'd' to the data frame but fails. What is the error?
R Programming
df <- data.frame(a = 1:3, b = 4:6)
df$d <- c(7, 8)
print(df)
ANo error, column 'd' added with NA for missing values
BError: replacement has 2 rows, data has 3
CError: object 'd' not found
DColumn 'd' added with recycled values
Attempts:
2 left
💡 Hint
Check if the length of the new column matches the number of rows.
❓ Predict Output
advanced
2:00remaining
Output after removing multiple columns with subset
What is the output of this R code after removing columns 'b' and 'c' using subset?
R Programming
df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
df2 <- subset(df, select = -c(b, c))
print(df2)
A
  a
1 1
2 2
3 3
B
  a b c
1 1 4 7
2 2 5 8
3 3 6 9
CError in subset(df, select = -c(b, c)) : invalid subscript type
D
  b c
1 4 7
2 5 8
3 6 9
Attempts:
2 left
💡 Hint
The select = -c(...) syntax removes specified columns.
🧠 Conceptual
expert
2:00remaining
Why is using transform() safer than direct assignment for adding columns?
Which reason best explains why transform() is often preferred over direct assignment (e.g., df$newcol <- ...) when adding columns to a data frame?
ABecause <code>transform()</code> returns a new data frame and avoids modifying the original by reference
BBecause direct assignment always causes errors if the new column length differs
CBecause <code>transform()</code> automatically removes NA values from the new column
DBecause direct assignment cannot add columns with names longer than 5 characters
Attempts:
2 left
💡 Hint
Think about how data frames are copied or modified in R.

Practice

(1/5)
1. In R, how do you add a new column named age with values c(25, 30, 35) to an existing dataframe df?
easy
A. df <- c(df, age = c(25, 30, 35))
B. df[age] = c(25, 30, 35)
C. df$age <- c(25, 30, 35)
D. df$age = NULL

Solution

  1. Step 1: Understand how to add columns in R

    In R, you add a new column to a dataframe by assigning a vector to df$column_name.
  2. Step 2: Apply the assignment for the new column

    Assigning c(25, 30, 35) to df$age adds the new column age with those values.
  3. Final Answer:

    df$age <- c(25, 30, 35) -> Option C
  4. Quick Check:

    Use $ and assign vector to add column [OK]
Hint: Use df$column <- values to add columns fast [OK]
Common Mistakes:
  • Using NULL to add a column instead of removing
  • Trying to add column with c() outside assignment
  • Using incorrect dataframe syntax like c(df, ...)
2. Which of the following is the correct syntax to remove the column height from dataframe df in R?
easy
A. df$height <- NULL
B. df$height = c()
C. df$height <- NA
D. remove(df$height)

Solution

  1. Step 1: Recall how to remove columns in R

    Assigning NULL to a dataframe column removes it completely.
  2. Step 2: Apply removal syntax

    Setting df$height <- NULL deletes the height column from df.
  3. Final Answer:

    df$height <- NULL -> Option A
  4. Quick Check:

    Assign NULL to remove column [OK]
Hint: Remove columns by assigning NULL: df$col <- NULL [OK]
Common Mistakes:
  • Assigning empty vector c() instead of NULL
  • Assigning NA which keeps column but fills with missing
  • Using remove() which is not for dataframe columns
3. What will be the output of the following R code?
df <- data.frame(name = c("Ann", "Bob"), score = c(90, 85))
df$passed <- c(TRUE, FALSE)
df$score <- NULL
df
medium
A. A dataframe with columns: name, passed
B. A dataframe with columns: name, score, passed
C. A dataframe with columns: name, score
D. Error because score column cannot be removed

Solution

  1. Step 1: Add new column 'passed'

    The code adds a logical column passed with values TRUE and FALSE.
  2. Step 2: Remove the 'score' column

    Assigning NULL to df$score deletes the score column.
  3. Final Answer:

    A dataframe with columns: name, passed -> Option A
  4. Quick Check:

    Added passed, removed score, left name [OK]
Hint: NULL removes columns; new assignment adds columns [OK]
Common Mistakes:
  • Thinking score column remains after NULL assignment
  • Expecting error on removing existing column
  • Confusing TRUE/FALSE vector with numeric
4. Consider this R code snippet:
df <- data.frame(x = 1:3, y = 4:6)
df$z = c(7, 8)
df$y <- NULL
df

What is the problem and how to fix it?
medium
A. Error because data.frame cannot have NULL columns; fix by using list
B. No error; code runs fine and removes y column
C. Error because NULL cannot remove columns; fix by using NA
D. Warning because length of z vector does not match rows; fix by matching length

Solution

  1. Step 1: Check length of new column vector

    The dataframe has 3 rows, but c(7, 8) has length 2, causing a length mismatch warning.
  2. Step 2: Fix by providing vector of correct length

    Use c(7, 8, 9) or recycle values properly to match 3 rows.
  3. Final Answer:

    Warning due to length mismatch; fix by matching vector length -> Option D
  4. Quick Check:

    New column vector length must match dataframe rows [OK]
Hint: New column vector length must equal dataframe rows [OK]
Common Mistakes:
  • Ignoring vector length mismatch causing warning
  • Thinking NULL cannot remove columns
  • Trying to assign NA to remove columns
5. You have a dataframe df with columns id, score1, and score2. You want to create a new column average that is the mean of score1 and score2, then remove score1 and score2. Which code correctly does this?
hard
A. df$average <- mean(df$score1, df$score2) df$score1 <- NULL df$score2 <- NULL
B. df$average <- rowMeans(df[c("score1", "score2")]) df$score1 <- NULL df$score2 <- NULL
C. df$average <- mean((df$score1 + df$score2) / 2) df$score1 <- NULL df$score2 <- NULL
D. df$average <- (score1 + score2) / 2 df$score1 <- NULL df$score2 <- NULL

Solution

  1. Step 1: Calculate average of two columns row-wise

    Use rowMeans() on the subset df[c("score1", "score2")] to get the mean for each row.
  2. Step 2: Remove the original score columns

    Assign NULL to df$score1 and df$score2 to delete them.
  3. Final Answer:

    df$average <- rowMeans(df[c("score1", "score2")]) df$score1 <- NULL df$score2 <- NULL -> Option B
  4. Quick Check:

    Use rowMeans() for row-wise average, then remove columns [OK]
Hint: Use rowMeans() for row averages, then assign NULL to remove [OK]
Common Mistakes:
  • Using mean() which computes overall mean, not row-wise
  • Referencing columns without df$ prefix
  • Trying to remove columns by assigning NA
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