Row and column indexing in R Programming - Time & Space Complexity
We want to understand how the time it takes to get rows and columns from a data frame changes as the data grows.
How does the work grow when we ask for more rows or columns?
Analyze the time complexity of the following code snippet.
# Create a data frame with n rows and m columns
n <- 1000
m <- 50
DF <- data.frame(matrix(1:(n*m), nrow = n, ncol = m))
# Extract a single row
row_10 <- DF[10, ]
# Extract a single column
col_5 <- DF[, 5]
# Extract multiple rows and columns
subset <- DF[1:100, 1:10]
This code creates a data frame and extracts rows and columns using indexing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing elements by row and column indexes involves reading data from memory.
- How many times: For a single row or column, it reads all elements in that row or column once. For multiple rows and columns, it reads all selected elements.
When you get one row, the work depends on how many columns there are. When you get one column, the work depends on how many rows there are.
| Input Size (n rows, m columns) | Approx. Operations for one row | Approx. Operations for one column |
|---|---|---|
| 10 rows, 5 columns | 5 reads | 10 reads |
| 100 rows, 50 columns | 50 reads | 100 reads |
| 1000 rows, 100 columns | 100 reads | 1000 reads |
Pattern observation: The time grows linearly with the number of elements you extract, either by row or column.
Time Complexity: O(k)
This means the time grows directly with the number of elements you select (k), whether from rows or columns.
[X] Wrong: "Extracting a single row or column always takes the same time regardless of size."
[OK] Correct: The time depends on how many elements are in that row or column, so bigger rows or columns take more time.
Understanding how data access time grows helps you write efficient code and explain your choices clearly in real projects or interviews.
"What if we extract multiple non-contiguous rows and columns using a vector of indexes? How would the time complexity change?"
Practice
data[2, 3] select from a data frame named data?Solution
Step 1: Understand the indexing format
In R,data[row, column]selects the element at the specified row and column.Step 2: Apply the given indices
Here, row = 2 and column = 3, so it selects the value at 2nd row and 3rd column.Final Answer:
The value in the 2nd row and 3rd column -> Option DQuick Check:
data[2, 3] = value at row 2, column 3 [OK]
- Confusing row and column order
- Thinking it selects entire row or column
- Using parentheses instead of brackets
df in R?Solution
Step 1: Recall indexing rules for rows and columns
Leaving the row index blank selects all rows; specifying a column index selects that column.Step 2: Match syntax to select all rows and 4th column
df[, 4]means all rows, 4th column, which is correct.Final Answer:
df[, 4] -> Option AQuick Check:
Blank row index means all rows [OK]
- Using df[4, ] selects 4th row, not column
- Using df[4] selects 4th column but drops dimensions
- Using df[4, 4] selects single cell, not all rows
df <- data.frame(A=1:3, B=4:6, C=7:9), what is the output of df[1:2, 2]?Solution
Step 1: Understand the data frame structure
df has columns A=1,2,3; B=4,5,6; C=7,8,9.Step 2: Select rows 1 and 2, column 2
Rows 1 and 2 of column B (2nd column) are 4 and 5.Final Answer:
[1] 4 5 -> Option BQuick Check:
df[1:2, 2] = c(4, 5) [OK]
- Confusing column numbers with letters
- Selecting wrong rows or columns
- Expecting output as a data frame instead of vector
df?
df[3]
Solution
Step 1: Understand single bracket indexing with one index
Using one index likedf[3]selects the 3rd column, not row.Step 2: Identify correct way to select 3rd row
To select 3rd row and all columns, usedf[3, ].Final Answer:
It selects the 3rd column, not the 3rd row -> Option AQuick Check:
df[3] = 3rd column, not row [OK]
- Using single index to select rows
- Expecting df[3] to select 3rd row
- Confusing data frame and vector indexing
df with 5 rows and 4 columns named A, B, C, D. How do you select rows 2 to 4 and columns B and D using row and column indexing?Solution
Step 1: Identify rows and columns to select
Rows 2 to 4 means indices 2, 3, 4; columns B and D are selected by names "B" and "D".Step 2: Use correct syntax for row and column indexing
Usedf[2:4, c("B", "D") ]to select rows 2-4 and columns named B and D.Final Answer:
df[2:4, c("B", "D") ] -> Option CQuick Check:
Row indices and column names combined correctly [OK]
- Using numeric column indices instead of names
- Passing column names as numbers in quotes
- Selecting wrong rows or columns
Ready to prove it?
Pass the test and this pattern moves unknown
