convert data into matrix- awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert data into matrix- awk
# 15  
Old 02-29-2012
Quote:
Originally Posted by daWonderer
yes. and we have different keys for combinations.
it was asked for the amount of different keys for each if I understood correctly..
Code:
awk '{f1[$3]++;f2[$2]++;a[$3"^"$2]++;}END
[snip...]

f1 is the different cell and f2 is different keys in the input.

Cheers,
RangaSmilie
# 16  
Old 02-29-2012
@Ranga: I think cell3 states are wrong in the output of your code.
# 17  
Old 02-29-2012
awk

Quote:
Originally Posted by quincyjones
is it possible to count the number of keys based on state and cell and output it as a simple matrix.

Ex: cell1-state1 has 2 keys
cell3-state1 has 4 keys.

Note: Insert 0 if no data available.

input

Code:
key    states    cell
key1    state1    cell1
key1    state2    cell1
key1    state3    cell1
key1    state4    cell1
key1    state5    cell1
key1    state6    cell1
key1    state7    cell1
key1    state8    cell1
key1    state9    cell1
key1    state10    cell1
key1    state11    cell1
key1    state12    cell1
key1    state13    cell1
key1    state14    cell1
key1    state15    cell1
key2    state1    cell1
key2    state2    cell1
key2    state3    cell1
key2    state4    cell1
key2    state5    cell1
key2    state6    cell1
key2    state7    cell1
key2    state8    cell1
key2    state9    cell1
key2    state10    cell1
key2    state11    cell1
key2    state12    cell1
key2    state13    cell1
key2    state14    cell1
key2    state15    cell1
key1    state1    cell2
key1    state2    cell2
key1    state3    cell2
key1    state4    cell2
key1    state5    cell2
key1    state6    cell2
key1    state7    cell2
key1    state8    cell2
key1    state9    cell2
key1    state10    cell2
key1    state11    cell2
key1    state12    cell2
key1    state13    cell2
key1    state14    cell2
key1    state15    cell2
key1    state1    cell3
key1    state14    cell3
key2    state1    cell3
key3    state1    cell3
key4    state1    cell3

output
Code:
cell    state1    state2    state3    state4    state5    state6    state7    state8    state9    state9    state10    state11    state12    state13    state14    state15
cell1    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
cell2    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
cell3   4    0    0    0    0    0    0    0    0    0    0   1    0   1    0

Your expected output is wrong, just check that we dont have key for state12 and cell3. is that typo error ?

Code:
key1    state1    cell3
key1    state14    cell3
key2    state1    cell3
key3    state1    cell3
key4    state1    cell3

You mean 0 if the key is not found,
Yeah i have missed that requirment.

Note: Insert 0 if no data available.

Check with the below code for that requirment.

Code:
awk '{f1[$3]++;f2[$2]++;a[$3"^"$2]++;}END{ORS="";print "      ";for(j in f2){print j" ";c++;if(c>5){print " ";}}print "\n";for(i in f1){print i;for(k in f2){l=i"^"k;if(a[l] == ""){print "   0    ";}else{print "   "a[l]"    ";}}print "\n";}}' Input_File

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 18  
Old 02-29-2012
Quote:
Originally Posted by rangarasan
Code:
awk '{f1[$3]++;f2[$2]++;a[$3"^"$2]++;}END
[snip...]

that's confusing to me. I just noticed you didn't used or checked for '$1' (col1 contains keys?)
how to not increase without check if same key for a combination occurred again?

Last edited by daWonderer; 02-29-2012 at 05:21 AM.. Reason: some grammar
# 19  
Old 02-29-2012
Also can you add this minor change please. normalize the output matrix by dividing total no.of unique keys * 100 ? with a different command by piping

command1 input | command2

For ex: cell1-state1 (2/4)*100
# 20  
Old 02-29-2012
awk

Quote:
Originally Posted by daWonderer
that's confusing to me. I just noticed you didn't used or checked for '$1' (col1 contains keys?)
how to not increase without check if same key for a combination occurred again?
Code:
f1[$3]++;f2[$2]++;

we are not using the above arrays count of particular keys. even we can use the below

Code:
f1[$3]=1;f2[$2]=1;

yeah you have got point that if i have same key exist more than once for the same combination of cell and state

Code:
key1    state1    cell3
key1    state14    cell3
key2    state1    cell3
key3    state1    cell3
key4    state1    cell3
key4    state1    cell3

The above code will display the count as 5 for cell3 and state1 but the actual count count is 4 bcos the same key is exist. but thats not in the input which we have too. if its the case, check with the below code,

Code:
awk '{
f1[$3]=1;f2[$2]=1;if(chk[$3"^"$2] != $1){a[$3"^"$2]++;chk[$3"^"$2]=$1;}
}END{ORS="";print "      ";for(j in f2){print j" ";c++;if(c>5){print " ";}}print "\n";for(i in f1){print i;for(k in f2){l=i"^"k;if(a[l] == ""){print "   0    ";}else{print "   "a[l]"    ";}}print "\n";}}' Input_File

Cheers,
RangaSmilie

---------- Post updated at 04:49 AM ---------- Previous update was at 04:44 AM ----------

Quote:
Originally Posted by quincyjones
Also can you add this minor change please. normalize the output matrix by dividing total no.of unique keys * 100 ? with a different command by piping

command1 input | command2

For ex: cell1-state1 (2/4)*100
2 is count of keys.
4 is total no of keys.

right ?

where you need this output ?
Expected output format ?

Cheers,
Ranga Smilie
# 21  
Old 02-29-2012
yes exactly.
I need the count when I run the command1 and then the second command should normalize. The output should be the same matrix format as before.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to convert 2 columns into matrix -awk?

How can i convert two columns in to o and 1 matrix. thnks Input a c1 b c2 c c1 d c3 e c4 output c1 c2 c3 c4 a 1 0 0 0 b 0 1 0 0 c 1 0 0 0 d 0 0 ... (5 Replies)
Discussion started by: quincyjones
5 Replies

2. Shell Programming and Scripting

Convert data into tabular matrix

Hi There, I want to convert the following data into tabular matrix, based on column 4th and 5th, and output the column 10th value chr1 2804449 2804450 NACpG_1 window1 + chr1 2804443 2804475 1 chr1 2804450 2804451 NACpG_1 window2 + chr1 2804443 ... (3 Replies)
Discussion started by: ChiragNepal
3 Replies

3. Shell Programming and Scripting

How order a data matrix using awk?

is it possible to order the following row clusters from ascending to descending. thanx in advance input 1 2 4 0 1 2 4 0 3 3 3 3 1 5 1 0 1 5 1 0 6 0 0 0 5 1 1 1... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

5. UNIX for Dummies Questions & Answers

Convert nxm Matrix into columns of data

Dear Unixers, I'm having some difficulty in converting an n x m data matrix into a dataset of 3 columns and nxm rows. As an example I want to convert this dataset 2 3 4 5 2 0.0 0.0 0.1 0.1 6 -0.3 2.0 0.0 0.3 7 -0.6 -1.1 0.5 0.3 9 -0.9 -4.1 -0.7 0.5 ... (2 Replies)
Discussion started by: tintin72
2 Replies

6. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

7. Shell Programming and Scripting

two-column data to matrix in AWK

Howdy, I need to convert an association data matrix, currently in a two-column format, into a matrix with numbers indicating the number of associations. I've been looking around for AWK code in the list, but could not find anything. Here's an example of what I want to perform: original... (10 Replies)
Discussion started by: sramirez
10 Replies

8. Shell Programming and Scripting

By using AWK can I convert matrice shaped data to a row ?

Hello, I have output in the matrice form , for example: 1 2 3 4 a b c d jim joe sue tom how can I convert this line-column data into a row as follows 1 2 3 4 a b c d jim joe sue tom thank you (14 Replies)
Discussion started by: rpf
14 Replies
Login or Register to Ask a Question