0
0
R Programmingprogramming~20 mins

Row and column indexing in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Row and Column Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
  A  C
2 2 10
3 3 11
B
  A  C
1 1 9
4 4 12
C
  B  C
2 6 10
3 7 11
D
  A  B
2 2 6
3 3 7
Attempts:
2 left
💡 Hint
Remember that df[row_indices, column_indices] selects rows and columns by their positions.
Predict Output
intermediate
2: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)
A
   X  Y  Z
2 20 15  2
3 30 25  3
B
   X  Z
2 20  2
3 30  3
C
   Y
2 15
3 25
D
   X  Z
1 10  1
2 20  2
3 30  3
Attempts:
2 left
💡 Hint
Logical vectors can be used to select columns; TRUE means select that column.
🔧 Debug
advanced
2: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)
AError: subscript out of bounds
BNo error, prints 1 2 3 4
CError: object 'result' not found
DNo error, prints 1 2 3 NA
Attempts:
2 left
💡 Hint
Check how R handles indexing beyond the number of rows in a data frame.
Predict Output
advanced
2: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)
A
  M  O
1 1 11
5 5 15
B
  N  O
1 6 11
5 10 15
C
  M  N
1 1 6
5 5 10
D
  M  O
2 2 12
3 3 13
4 4 14
Attempts:
2 left
💡 Hint
Negative indices exclude the specified rows or columns.
🧠 Conceptual
expert
2: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)
A3
B9
C6
D15
Attempts:
2 left
💡 Hint
Count rows selected times columns selected.