This proposal does not depend on column 1 being sorted (as others above do), it will collect lines correctly into the matrix elements even if the key comes up again later with more values. It would not, of course, have the across columns sorted, then. Empty/missing elements are leaving a gap in the output:
How to run it or how it works internally?
1) copy the code from my post to your command line and replace "file" with your filename.
2)
{for (i=1; i<=LnCnt; i++) if ($1 == Ln) ... check and retain unique row keys.
{for (j=1; j<=HdCnt; j++) if ($2 == Hd[j]) ... check and retain unique col leys.
{Mx[$1,$2] = $3} ... fill matrix elements depending on row & col value.
END ... print out header line and print all collected rows (loop i) with the resp. col (loop j) values
RudiC, good idea - should have thought of it myself.
Here's my second take: not depending on column 1 being sorted; taking of the potential gaps/missing rows.
awk 'BEGIN {
OFS="\t"
}
{
row[$1,$2]=$3
w=(length($2)>w)?length($2):w
if (!($2 in f2)) { header=(header)?header OFS $2:$2;f2[$2]}
if (!($1 in col1a)) {
col1[++c]=$1
col1a[$1]
}
}
END {
empty=sprintf("%*s",w," ")
printf("%*s%s\n", length(col1[1])+2, " ",header)
ncol=split(header,colA,OFS)
for(i=1;i<=c;i++) {
printf("%s", col1)
for(j=1;j<=ncol;j++) {
idx=(col1 SUBSEP colA[j])
printf("%s%s%c", OFS, (idx in row)?row[idx]:empty, (j==ncol)?ORS:"")
}
}
}' myFile