Challenge - 5 Problems
Column Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that
cbind adds columns side by side and keeps row order.✗ Incorrect
The
cbind function adds the new column 'c' to the existing data frame, matching rows by position.❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Assigning NULL to a column removes it from the data frame.
✗ Incorrect
Setting
df$b <- NULL deletes the column 'b' from the data frame, leaving only 'a' and 'c'.🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if the length of the new column matches the number of rows.
✗ Incorrect
The new column vector has length 2 but the data frame has 3 rows, causing a length mismatch error.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
The
select = -c(...) syntax removes specified columns.✗ Incorrect
Using
subset with negative selection removes columns 'b' and 'c', leaving only 'a'.🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Think about how data frames are copied or modified in R.
✗ Incorrect
transform() returns a new data frame with the added or modified columns, which helps avoid side effects on the original data frame.