Tidy-TS Logo

Transposing Data

Flip rows and columns to change your data perspective. Perfect for time series analysis, visualization, and preparing data for different analysis tools.

Basic Transpose
Flip rows and columns with a simple transpose operation

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";

const sales = createDataFrame([
  { product: "Widget A", q1: 100, q2: 120, q3: 110, q4: 130 },
  { product: "Widget B", q1: 80, q2: 90, q3: 95, q4: 105 },
]);

// Transpose rows and columns
const transposed = sales.transpose({ numberOfRows: 2 });
console.log("Transposed data:");
transposed.print();
import { createDataFrame } from "@tidy-ts/dataframe";

const sales = createDataFrame([
  { product: "Widget A", q1: 100, q2: 120, q3: 110, q4: 130 },
  { product: "Widget B", q1: 80, q2: 90, q3: 95, q4: 105 },
]);

// Transpose rows and columns
const transposed = sales.transpose({ numberOfRows: 2 });
console.log("Transposed data:");
transposed.print();
Custom Row 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. This is especially useful for time series data.

// Add custom row labels before transposing
const withLabels = sales.setRowLabels(["widget_a", "widget_b"]);
const labeledTranspose = withLabels.transpose({ numberOfRows: 2 });

console.log("Transposed with custom labels:");
labeledTranspose.print();
// Add custom row labels before transposing
const withLabels = sales.setRowLabels(["widget_a", "widget_b"]);
const labeledTranspose = withLabels.transpose({ numberOfRows: 2 });

console.log("Transposed with custom labels:");
labeledTranspose.print();
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 without any data loss.

// Double transpose returns to original structure
const backToOriginal = transposed.transpose({ numberOfRows: 2 });
console.log("Double transpose (restored):");
backToOriginal.print();
// Double transpose returns to original structure
const backToOriginal = transposed.transpose({ numberOfRows: 2 });
console.log("Double transpose (restored):");
backToOriginal.print();
Mixed Data Types
Transpose works with any data types including arrays and objects

Transpose operations work with any data types including strings, numbers, booleans, and even complex types like arrays and objects. All types are preserved perfectly.

// Transpose works with mixed data types
const mixed = createDataFrame([
  { id: 1, name: "Alice", active: true, score: 95.5 },
  { id: 2, name: "Bob", active: false, score: 87.2 },
]);

const mixedTranspose = mixed.setRowLabels(["user1", "user2"]).transpose({ numberOfRows: 2 });
console.log("Mixed data types transpose:");
mixedTranspose.print();
// Transpose works with mixed data types
const mixed = createDataFrame([
  { id: 1, name: "Alice", active: true, score: 95.5 },
  { id: 2, name: "Bob", active: false, score: 87.2 },
]);

const mixedTranspose = mixed.setRowLabels(["user1", "user2"]).transpose({ numberOfRows: 2 });
console.log("Mixed data types transpose:");
mixedTranspose.print();