Challenge - 5 Problems
Data Frame Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of data frame column extraction
What is the output of this R code snippet?
R Programming
df <- data.frame(name = c("Anna", "Ben"), age = c(25, 30)) result <- df$name print(result)
Attempts:
2 left
💡 Hint
Remember that $ extracts a column as a vector from a data frame.
✗ Incorrect
Using df$name extracts the 'name' column as a vector of strings. So printing it shows the names Anna and Ben.
🧠 Conceptual
intermediate2:00remaining
Why data frames are preferred for tabular data in R
Which reason best explains why data frames are central to R for handling tabular data?
Attempts:
2 left
💡 Hint
Think about how real-world tables have different types of information in columns.
✗ Incorrect
Data frames can hold different data types in each column (numbers, text, factors) but keep rows aligned, making them ideal for real-world tabular data.
🔧 Debug
advanced2:00remaining
Identify the error in data frame creation
What error does this R code produce?
R Programming
df <- data.frame(name = c("Anna", "Ben"), age = c(25, 30, 35))
Attempts:
2 left
💡 Hint
Check if all columns have the same length.
✗ Incorrect
Data frames require all columns to have the same number of rows. Here, 'name' has 2 elements but 'age' has 3, causing an error.
❓ Predict Output
advanced2:00remaining
Result of subsetting a data frame
What is the output of this R code?
R Programming
df <- data.frame(name = c("Anna", "Ben", "Cara"), age = c(25, 30, 22)) subset_df <- df[df$age > 23, ] print(subset_df)
Attempts:
2 left
💡 Hint
Rows where age is greater than 23 are kept.
✗ Incorrect
The code filters rows where age > 23, which are Anna (25) and Ben (30). Cara (22) is excluded.
🧠 Conceptual
expert3:00remaining
Why data frames support R's statistical functions
Why are data frames essential for R's statistical analysis capabilities?
Attempts:
2 left
💡 Hint
Think about how statistical models refer to variables by their names.
✗ Incorrect
Data frames let statistical functions access columns by name and handle different data types, which is crucial for modeling and analysis.