Recoding data in a matrix from an existing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recoding data in a matrix from an existing file
# 1  
Old 11-19-2013
Recoding data in a matrix from an existing file

Hi, I was wondering if someone would be able to help with extrapolating information from a file and filling an existing matrix with that information.

I have made a matrix like this (file 1):
Code:
  A B C D  
1
2
3
4

I have another file with data like this (file 2):

Code:
1 A
1 C
3 C
4 B
4 D

I'd like to take the data and recode it where it looks like this, whereby if the information in file 2 exists, I'd like it to be marked as a "Y" in the matrix, and "N" if it is missing.
The desired output looks like this:
Code:
  A B C D  
1 Y N Y N 
2 N N N N 
3 N N Y N 
4 N Y N Y

My actual file has about 600 columns and 500 rows. Does anyone have a good idea regarding how to do this? Any help would be greatly appreciated!

---------- Post updated at 07:15 PM ---------- Previous update was at 06:43 PM ----------

Please ignore, I found a working solution after a more refined search through the help archives. Cheers
# 2  
Old 11-19-2013
You could also try this:

Code:
awk '
NR==1{
   T=$0;
   for(i=1;i<=NF;i++) H[$i]
   next}
FNR==NR{F[NR]=$1;R[$1]; next}
$1 in R && $2 in H {G[$1,$2]}
END{
   print T;
   c=split(T, H);
   for(i=2;i<=FNR;i++) {
     $1=F[i]
     for(j=1;j<=c;j++) $(j+1) = ($1 SUBSEP H[j] in G)?"Y":"N"
     print
   }
}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

2. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

3. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

4. 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

5. Shell Programming and Scripting

add more data to existing data in a file

Hi all, I need help to add additional data from file2 to existing data in file 1 using awk, sed or perl. the ID in file 1 should match against field $3 in file2 file1 #this is a new game ID HR_1 BASE1 30 BASE2 37 DETAIL No TYPE L @@ ID HR_10 BASE1 6030 BASE2 ... (4 Replies)
Discussion started by: redse171
4 Replies

6. Shell Programming and Scripting

How to delete already existing data in a file using perl? Pls help me!!

Dear Friends, I need urgent help from u.. I have two files,file1 & file 2.. file1 have a existing data of file2.So i want to delete those existing datas from file1 (which contain the data from file1) My file1 like this rs39348 1 1045729 A G 0.1791 0.2054 0.84 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

7. Shell Programming and Scripting

recoding data points using SED??

Hello all, I have a data file that needs some serious work...I have no idea how to implement the changes that are needed! The file is a genotypic file with >64,000 columns representing genetic markers, a header line, and >1100 rows that looks like this: ID 1 2 3 4 ... (7 Replies)
Discussion started by: doobedoo
7 Replies

8. UNIX for Dummies Questions & Answers

Add line with data to existing file

i have a file called motors with 3 columns separated with tabs eg: car make reg i want to create a executable file that will add a line with data eg: car make reg benz s600 t35778 can you please show me how to do this? (7 Replies)
Discussion started by: fletcher
7 Replies

9. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies

10. Shell Programming and Scripting

Need to add a line of data to already existing file in Unix..

Hello.. I have a text file with 100 lines of data. I need to add 1 line of data to that already existing file at the first line( beginning of the file) , so that the already existing 100 lines will start from 2 nd line.Now the file will have 101 lines of data. Help me on how to add the line... (4 Replies)
Discussion started by: charan81
4 Replies
Login or Register to Ask a Question