Flip rows and columns to change your data perspective. Perfect for time series analysis, visualization, and preparing data for different analysis tools.
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();
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();
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();
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();