Tidy-TS Logo

Reshaping Data

Transform your data between long and wide formats with pivot operations and transpose data for different analysis needs. Learn how to reshape data effectively.

Pivot Wider (Long to Wide)
Convert long format data to wide format with products as columns

pivotWider transforms long format data to wide format, making it easier to compare values across categories and prepare data for analysis or visualization.

import { createDataFrame } from "@tidy-ts/dataframe";

// Create sales data in long format (one row per product per quarter)
const salesLong = createDataFrame([
  { year: 2023, quarter: "Q1", product: "Widget A", sales: 1000 },
  { year: 2023, quarter: "Q1", product: "Widget B", sales: 1500 },
  { year: 2023, quarter: "Q2", product: "Widget A", sales: 1200 },
  { year: 2023, quarter: "Q2", product: "Widget B", sales: 1800 },
]);

// Pivot to wide format (one row per quarter, columns for each product)
const salesWide = salesLong.pivotWider({
  namesFrom: "product",
  valuesFrom: "sales",
  expectedColumns: ["Widget A", "Widget B"],
});

salesWide.print("Sales data pivoted to wide format:");
import { createDataFrame } from "@tidy-ts/dataframe";

// Create sales data in long format (one row per product per quarter)
const salesLong = createDataFrame([
  { year: 2023, quarter: "Q1", product: "Widget A", sales: 1000 },
  { year: 2023, quarter: "Q1", product: "Widget B", sales: 1500 },
  { year: 2023, quarter: "Q2", product: "Widget A", sales: 1200 },
  { year: 2023, quarter: "Q2", product: "Widget B", sales: 1800 },
]);

// Pivot to wide format (one row per quarter, columns for each product)
const salesWide = salesLong.pivotWider({
  namesFrom: "product",
  valuesFrom: "sales",
  expectedColumns: ["Widget A", "Widget B"],
});

salesWide.print("Sales data pivoted to wide format:");
Pivot Longer (Wide to Long)
Convert wide format data to long format for analysis

pivotLonger (also called 'melting') converts wide format data to long format, which is often required for statistical analysis and visualization libraries.

// Create student grade data in wide format (one column per subject)
const gradesWide = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
  { id: 3, name: "Charlie", math: 78, science: 95, english: 92 },
]);

// Melt to long format (one row per student per subject)
const gradesLong = gradesWide.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

gradesLong.print("Student grades melted to long format:");
// Create student grade data in wide format (one column per subject)
const gradesWide = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
  { id: 3, name: "Charlie", math: 78, science: 95, english: 92 },
]);

// Melt to long format (one row per student per subject)
const gradesLong = gradesWide.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

gradesLong.print("Student grades melted to long format:");
Basic Transpose
Flip rows and columns for different analysis perspectives

Transpose operations flip rows and columns, making it easy to reshape data for different analysis needs. tidy-ts provides reversible transposes with strong type preservation.

import { createDataFrame } from "@tidy-ts/dataframe";

// Create sales data in long format (one row per product per quarter)
const salesLong = createDataFrame([
  { year: 2023, quarter: "Q1", product: "Widget A", sales: 1000 },
  { year: 2023, quarter: "Q1", product: "Widget B", sales: 1500 },
  { year: 2023, quarter: "Q2", product: "Widget A", sales: 1200 },
  { year: 2023, quarter: "Q2", product: "Widget B", sales: 1800 },
]);

// Pivot to wide format (one row per quarter, columns for each product)
const salesWide = salesLong.pivotWider({
  namesFrom: "product",
  valuesFrom: "sales",
  expectedColumns: ["Widget A", "Widget B"],
});

salesWide.print("Sales data pivoted to wide format:");
import { createDataFrame } from "@tidy-ts/dataframe";

// Create sales data in long format (one row per product per quarter)
const salesLong = createDataFrame([
  { year: 2023, quarter: "Q1", product: "Widget A", sales: 1000 },
  { year: 2023, quarter: "Q1", product: "Widget B", sales: 1500 },
  { year: 2023, quarter: "Q2", product: "Widget A", sales: 1200 },
  { year: 2023, quarter: "Q2", product: "Widget B", sales: 1800 },
]);

// Pivot to wide format (one row per quarter, columns for each product)
const salesWide = salesLong.pivotWider({
  namesFrom: "product",
  valuesFrom: "sales",
  expectedColumns: ["Widget A", "Widget B"],
});

salesWide.print("Sales data pivoted to wide format:");
Transpose with Custom Labels
Use meaningful row labels instead of generic names

You can provide custom row labels to make transposed data more meaningful and easier to work with in subsequent operations.

// Create student grade data in wide format (one column per subject)
const gradesWide = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
  { id: 3, name: "Charlie", math: 78, science: 95, english: 92 },
]);

// Melt to long format (one row per student per subject)
const gradesLong = gradesWide.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

gradesLong.print("Student grades melted to long format:");
// Create student grade data in wide format (one column per subject)
const gradesWide = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
  { id: 3, name: "Charlie", math: 78, science: 95, english: 92 },
]);

// Melt to long format (one row per student per subject)
const gradesLong = gradesWide.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

gradesLong.print("Student grades melted to long format:");
Double Transpose (Round-trip)
Transpose operations are perfectly reversible

Transpose operations are reversible with perfect data integrity. You can transpose data, perform analysis, and transpose back to the original structure.

// Start with wide format student data
const studentData = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
]);

console.log("Original wide format:");
studentData.print();

// First melt to long format
const longFormat = studentData.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

console.log("After melting to long format:");
longFormat.print();

// Then pivot back to wide with different structure
const backToWide = longFormat.pivotWider({
  namesFrom: "subject",
  valuesFrom: "score",
  expectedColumns: ["math", "science", "english"],
});

console.log("After pivoting back to wide format:");
backToWide.print();
// Start with wide format student data
const studentData = createDataFrame([
  { id: 1, name: "Alice", math: 85, science: 92, english: 78 },
  { id: 2, name: "Bob", math: 90, science: 88, english: 85 },
]);

console.log("Original wide format:");
studentData.print();

// First melt to long format
const longFormat = studentData.pivotLonger({
  cols: ["math", "science", "english"],
  namesTo: "subject",
  valuesTo: "score",
});

console.log("After melting to long format:");
longFormat.print();

// Then pivot back to wide with different structure
const backToWide = longFormat.pivotWider({
  namesFrom: "subject",
  valuesFrom: "score",
  expectedColumns: ["math", "science", "english"],
});

console.log("After pivoting back to wide format:");
backToWide.print();