0
0
R Programmingprogramming~20 mins

Data frame creation in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Frame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
  Name Age
1 Anna  25
2 Bob   30
B
  Name Age
1 Anna  30
2 Bob   25
C
  Name Age
1 25   Anna
2 30   Bob
DError: object 'df' not found
Attempts:
2 left
💡 Hint
Look at how vectors are assigned to columns in data.frame.
Predict Output
intermediate
1: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)
A1
B2
C4
DError: object 'df' not found
Attempts:
2 left
💡 Hint
nrow() returns the number of rows in a data frame.
🔧 Debug
advanced
2: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))
ABecause Age vector must be a factor, not numeric
BBecause the vectors for columns have different lengths
CBecause the Name vector is missing a closing quote
DBecause the data.frame function requires numeric columns only
Attempts:
2 left
💡 Hint
Check if all columns have the same number of elements.
🧠 Conceptual
advanced
2: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)
AA data frame with 2 rows and a list column where each cell contains a numeric vector
BA data frame with 2 rows and a numeric column with combined values
CAn error because data frames cannot have list columns
DA data frame with 4 rows, each with a single numeric value
Attempts:
2 left
💡 Hint
The I() function inhibits conversion and allows list columns.
Predict Output
expert
3: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)
A
   Name Age
1   Tom  51
2 Jerry  71
B
   Name Age
1   Tom   5
2 Jerry   7
CError: object 'df' not found
D
   Name Age
1   Tom   6
2 Jerry   8
Attempts:
2 left
💡 Hint
Adding 1 to the Age column increases each age by one.