Columns to Rows - Transpose - Special Condition

Hi Friends,

Hope all is well.

I have an input file like this

a gene1 10
b gene1 2
c gene2 20
c gene3 10
d gene4 5
e gene5 6

Steps to reach output.

  1. Print unique values of column1 as column of the matrix, which will be
a
b
c
d
e
  1. Print unique values of column2 as row of the matrix, which will be
gene1 gene2 gene3 gene4 gene5
  1. Now print column3 values in the matrix, which will be my final output
     gene1 gene2 gene3 gene4 gene5
a   10      0        0       0        0
b   2        0        0       0        0
c   0        20      10      0        0
d   0        0        0       5        0
e   0        0        0       0        6

My special condition is nothing but, if there is no value for a combination, print that value to be 0. My input file has more than 50K records. Please let me know if you have any questions. Thanks in advance.

The spacing is messed up while posting this thread. But, my output columns are separated by a tab.

With over 280 posts, we'd expect at least a hint of trying to resolve the issue.
Have you searched the forums for similar questions/issues?
Where exactly are you stuck?

1 Like

Yes, search and adapt. Has been solved before!

I have tried to make the matrix so far using the following commands.

awk '{print $2}' input.txt | awk '!x[$0]++' | tr '\n' '\t' > 1.temp

awk '{print $1}' input.txt | awk '!x[$0]++' > 2.temp

cat 1.temp 2.temp > matrix

I am unsure on how you match the row values to column values and print a zero when there is no match. Thanks for provoking to try. :slight_smile:

I'm not sure you really really searched these forums - you might have come across this and the one above it by vgersh99...

1 Like

Thanks Rudic. I was searching online and not in these forums. I will make it a point to do so from next time. It worked. Thank you again.