How do you perform data reshaping using tidyr functions like pivot_longer() and pivot_wider()?

Quick Overview

Asks how to reshape data in R with tidyr's pivot_longer() and pivot_wider(), moving between wide and long formats. It tests the key arguments, handling of missing values and duplicate keys, splitting compound column names, and when a round trip does not return the original table.

How do you perform data reshaping using tidyr functions like pivot_longer() and pivot_wider()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you perform data reshaping using `tidyr` functions like `pivot_longer()` and `pivot_wider()`? Explain what each one does to the shape of a data frame, the arguments that control it, and when you would use each. ```hint Track the rows and columns Take a small table with one row per store and one column per month. Write down how many rows and which columns each function produces, and which values become column names. ``` ```hint Where reshaping goes wrong Think about what `pivot_wider()` should do when two rows map to the same cell, or when a combination has no row at all. ``` ### What a Strong Answer Covers - What long and wide formats are, and which function goes in which direction - The key arguments of each function and what they control - A worked example with the input and the output shape - Missing values, duplicate keys and other cases where a round trip does not reproduce the original - When long format is the better shape for analysis or plotting ### Follow-up Questions - How would you reshape columns named like `sales_2023`, `sales_2024`, `units_2023` and `units_2024` into one row per year with `sales` and `units` columns? - What happens in `pivot_wider()` when two rows share the same id and name, and how do you resolve it? - When are `pivot_longer()` and `pivot_wider()` not exact inverses? - What are the equivalents in base R, `data.table` or pandas?

Overview: Asks how to reshape data in R with tidyr's pivot_longer() and pivot_wider(), moving between wide and long formats. It tests the key arguments, handling of missing values and duplicate keys, splitting compound column names, and when a round trip does not return the original table.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you perform data reshaping using tidyr functions like pivot_longer() and pivot_wider()? Explain what each one does to the shape of a data frame, the arguments that control it, and when you would use each.

What a Strong Answer Covers Guidance

  • What long and wide formats are, and which function goes in which direction
  • The key arguments of each function and what they control
  • A worked example with the input and the output shape
  • Missing values, duplicate keys and other cases where a round trip does not reproduce the original
  • When long format is the better shape for analysis or plotting

Follow-up Questions Guidance

  • How would you reshape columns named like sales_2023 , sales_2024 , units_2023 and units_2024 into one row per year with sales and units columns?
  • What happens in pivot_wider() when two rows share the same id and name, and how do you resolve it?
  • When are pivot_longer() and pivot_wider() not exact inverses?
  • What are the equivalents in base R, data.table or pandas?