Challenge - 5 Problems
Row and Column Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of selecting rows and columns by index
What is the output of the following R code?
R Programming
df <- data.frame(A = 1:4, B = 5:8, C = 9:12) result <- df[2:3, c(1,3)] print(result)
Attempts:
2 left
💡 Hint
Remember that df[row_indices, column_indices] selects rows and columns by their positions.
✗ Incorrect
The code selects rows 2 and 3, and columns 1 and 3 from the data frame. Column 1 is 'A' and column 3 is 'C'. Rows 2 and 3 have values 2,3 in 'A' and 10,11 in 'C'.
❓ Predict Output
intermediate2:00remaining
Output of logical indexing on rows and columns
What will be the output of this R code?
R Programming
df <- data.frame(X = c(10,20,30), Y = c(5,15,25), Z = c(1,2,3)) result <- df[df$X > 15, c(TRUE, FALSE, TRUE)] print(result)
Attempts:
2 left
💡 Hint
Logical vectors can be used to select columns; TRUE means select that column.
✗ Incorrect
Rows where X > 15 are rows 2 and 3. Columns selected are the first and third columns (X and Z) because the logical vector is TRUE, FALSE, TRUE.
🔧 Debug
advanced2:00remaining
Identify the error in row and column indexing
What error does this R code produce?
R Programming
df <- data.frame(A = 1:3, B = 4:6) result <- df[1:4, 1] print(result)
Attempts:
2 left
💡 Hint
Check how R handles indexing beyond the number of rows in a data frame.
✗ Incorrect
R allows indexing beyond existing rows and fills missing rows with NA values. So row 4 does not exist, but R returns NA for that row without error.
❓ Predict Output
advanced2:00remaining
Output of negative indexing on rows and columns
What is the output of this R code?
R Programming
df <- data.frame(M = 1:5, N = 6:10, O = 11:15) result <- df[-(2:4), -2] print(result)
Attempts:
2 left
💡 Hint
Negative indices exclude the specified rows or columns.
✗ Incorrect
Rows 2 to 4 are excluded, so only rows 1 and 5 remain. Column 2 (N) is excluded, so columns M and O remain.
🧠 Conceptual
expert2:00remaining
Number of items after complex indexing
Given the data frame below, how many elements are in the result of df[1:3, c(FALSE, TRUE, TRUE)]?
R Programming
df <- data.frame(A = 1:5, B = 6:10, C = 11:15)
Attempts:
2 left
💡 Hint
Count rows selected times columns selected.
✗ Incorrect
Rows 1 to 3 means 3 rows. The logical vector selects columns 2 and 3 (B and C), so 2 columns. Total elements = 3 * 2 = 6.