Challenge - 5 Problems
Data Frame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this data frame creation?
Consider the following R code that creates a data frame. What will be the output when printed?
R Programming
df <- data.frame(Name = c("Anna", "Bob"), Age = c(25, 30)) print(df)
Attempts:
2 left
💡 Hint
Look at how vectors are assigned to columns in data.frame.
✗ Incorrect
The data frame is created with two columns: Name and Age. The vectors are assigned in order, so Anna is 25 and Bob is 30.
❓ Predict Output
intermediate1:30remaining
What is the number of rows in this data frame?
Given this R code, how many rows does the data frame 'df' have?
R Programming
df <- data.frame(ID = 1:4, Score = c(88, 92, 79, 85)) nrow(df)
Attempts:
2 left
💡 Hint
nrow() returns the number of rows in a data frame.
✗ Incorrect
The data frame has 4 rows because the vectors have length 4.
🔧 Debug
advanced2:00remaining
Why does this data frame creation code produce an error?
Examine the code below. Why does it cause an error when run?
R Programming
df <- data.frame(Name = c("Alice", "Bob"), Age = c(30))
Attempts:
2 left
💡 Hint
Check if all columns have the same number of elements.
✗ Incorrect
In R, data frame columns must have equal length. Here, Name has length 2 but Age has length 1, causing an error.
🧠 Conceptual
advanced2:30remaining
What happens if you create a data frame with a list column?
Consider this R code creating a data frame with a list column. What is the structure of the resulting data frame?
R Programming
df <- data.frame(ID = 1:2, Values = I(list(c(1,2), c(3,4)))) str(df)
Attempts:
2 left
💡 Hint
The I() function inhibits conversion and allows list columns.
✗ Incorrect
Using I() wraps the list so the data frame keeps each list element as a cell, allowing complex data in columns.
❓ Predict Output
expert3:00remaining
What is the output of this complex data frame creation?
What will be printed when running this R code?
R Programming
df <- data.frame( Name = c("Tom", "Jerry"), Age = c(5, 7), stringsAsFactors = FALSE ) df$Age <- df$Age + 1 print(df)
Attempts:
2 left
💡 Hint
Adding 1 to the Age column increases each age by one.
✗ Incorrect
The Age column values are incremented by 1, so 5 becomes 6 and 7 becomes 8.