wider
Reshapes a table from long to wide format by spreading a key-value pair across
multiple columns. This is the inverse of longer and similar to crosstab.
Behavior:
- Converts long-format data to wide format by spreading columns.
- ID columns (specified by
--id-cols) are preserved and identify each row. - The
--names-fromcolumn values become the new column headers. - The
--values-fromcolumn values populate the new columns. - When multiple values map to the same cell, an aggregation operation is performed.
- Missing cells are filled with the value specified by
--values-fill(default: empty).
Input:
- Reads from one or more TSV files or standard input.
- Files ending in
.gzare transparently decompressed. - The first line is ALWAYS treated as a header.
- When multiple files are provided, they must have the same column structure.
Output:
- By default, output is written to standard output.
- Use
--outfile/-oto write to a file instead.
Header behavior:
- Supports
--header/-Hand--header-hash1modes. - The first line is always treated as a header to resolve column names.
Field syntax:
- Use
--names-fromto specify the column containing new column headers. - Use
--values-fromto specify the column containing data values. - Use
--id-colsto specify columns that identify each row. - Field lists support 1-based indices, ranges (
1-3,5-7), header names, name ranges (run-user_time), and wildcards (*_time). - Run
tva --help-fieldsfor a full description shared across tva commands.
Examples:
-
Spread
keyandvaluecolumns back into wide format tva wider –names-from key –values-from value data.tsv -
Spread
measurementcolumn, usingresultas values tva wider –names-from measurement –values-from result data.tsv -
Specify ID columns explicitly (dropping others) tva wider –names-from key –values-from val –id-cols id,date data.tsv
-
Count occurrences (crosstab) tva wider –names-from category –id-cols region –op count data.tsv
-
Calculate sum of values tva wider –names-from category –values-from amount –id-cols region –op sum data.tsv
-
Fill missing values with custom string tva wider –names-from key –values-from val –values-fill “NA” data.tsv
-
Sort resulting column headers alphabetically tva wider –names-from key –values-from val –names-sort data.tsv