0
0
R Programmingprogramming~5 mins

pivot_longer (wide to long) in R Programming - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: pivot_longer (wide to long)
O(n * k)
Understanding Time Complexity

When we change data from wide to long format using pivot_longer, it takes some time to rearrange the data.

We want to know how the time needed grows as the data gets bigger.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

library(tidyr)
data <- data.frame(
  id = 1:1000,
  score_math = rnorm(1000),
  score_english = rnorm(1000),
  score_science = rnorm(1000)
)

long_data <- pivot_longer(data, cols = starts_with("score_"),
                          names_to = "subject", values_to = "score")

This code takes a data frame with many score columns and turns it into a longer format with one score column and a subject column.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Going through each row and each selected column to reshape data.
  • How many times: For each of the n rows, it processes k columns (here 3 score columns).
How Execution Grows With Input

As the number of rows (n) or columns to pivot (k) grows, the work grows by multiplying these two.

Input Size (n rows)Approx. Operations (n * k)
1030 (10 rows * 3 columns)
100300 (100 * 3)
10003000 (1000 * 3)

Pattern observation: The time grows linearly with the number of rows and columns involved.

Final Time Complexity

Time Complexity: O(n * k)

This means the time needed grows proportionally to the number of rows times the number of columns you want to pivot.

Common Mistake

[X] Wrong: "pivot_longer only depends on the number of rows, so time grows linearly with rows only."

[OK] Correct: The function must also process each selected column for every row, so columns matter too.

Interview Connect

Understanding how data reshaping scales helps you handle bigger datasets efficiently and shows you think about performance in real tasks.

Self-Check

"What if we increased the number of columns to pivot from 3 to 10? How would the time complexity change?"