Sponsored Content
Full Discussion: Data imputation with scaling
Top Forums Shell Programming and Scripting Data imputation with scaling Post 302932634 by RavinderSingh13 on Friday 23rd of January 2015 01:39:23 AM
Old 01-23-2015
Hello senhia83,

Could you please try following too and let me know if this helps.
Code:
awk 'FNR==1{f++} FNR==NR{R[++h]=$1;sub(/[[:alpha:]]/,Z,$1);X[$1]=$2;next} (f==2){sub(/[[:alpha:]]/,Z,$1);{if(length($1)==1){Y[$1]=$2}}} (f==3){S=$1;Q=$2;sub(/[[:alpha:]]/,Z,$1);{if(length($1)>1){split($1, A,"");if(Y[A[1]] != "" && Y[A[2]] != ""){D=X[A[1]]+(X[A[2]] - X[A[1]]) * ($2 - Y[A[1]])/(Y[A[2]]-Y[A[1]])};print S OFS D;} else {print S OFS X[$1];m++}}} END{for(u in X){if(u==m){m++;print R[m] OFS X[m]}}}' base_file non_base_file non_base_file

Output will be as follows.
Code:
a1 10
b12 11.1905
c12 12.381
d12 14.7619
a2 15
b23 17.3239
a3 20
a4 21

I haven't tried with many senarios this code, please let me know if this helps.

EDIT: Adding non one liner form for same.
Code:
awk 'FNR==1     {f++}
     FNR==NR    {R[++h]=$1;sub(/[[:alpha:]]/,Z,$1);X[$1]=$2;next}
    (f==2)      {sub(/[[:alpha:]]/,Z,$1);
                                                        {if(length($1)==1)
                                                                                {Y[$1]=$2}
                                                        }
                }
     (f==3)     {S=$1;Q=$2;sub(/[[:alpha:]]/,Z,$1);     {if(length($1)>1)
                                                                                {split($1, A,"");
                                                                                 if(Y[A[1]] != "" && Y[A[2]] != ""){
                                                                                                                        D=X[A[1]]+(X[A[2]] - X[A[1]]) * ($2 - Y[A[1]])/(Y[A[2]]-Y[A[1]])}
                                                                                                                        ;print S OFS D;
                                                                                                                   }
                                                                                 else                              {
                                                                                                                        print S OFS X[$1];m++;
                                                                                                                   }
                                                        }
                }
     END        {for(u in X){
                                if(u==m)                {
                                                         print R[m] OFS X[m]
                                                         m++;
                                                        }
                           }
               }
   ' base_file non_base_file non_base_file

EDIT: Tried with following senario and it seems to be working fine.
Code:
cat non_base_file
a1 170
b12 175
c12 180
d12 190
a2  191
b23 567
a3  1000
a4  121
a5  675
a6  1100
f56 1200
 
 
cat base_file
a1 10
a2 15
a3 20
a4 21
a5 11
a6 17
a7 12
a8 123

Running the code we will get as follows.
Code:
./basic_non_basic1.ksh
a1 10
b12 11.1905
c12 12.381
d12 14.7619
a2 15
b23 17.3239
a3 20
a4 21
a5 11
a6 17
f56 18.4118
a7 12
a8 123

NOTE: Where basic_non_basic1.ksh is the above pasted script.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-23-2015 at 05:26 AM.. Reason: Added a non one liner form for solution
This User Gave Thanks to RavinderSingh13 For This Post:
 

We Also Found This Discussion For You

1. Shell Programming and Scripting

Re-scaling values - perl

Hey folks I have a big tab delimited file with 3 columns looks like this: chr2L 552 0.85 chr2R 135 1.06 chr3L 820 2.89 chr3R 581 3.93 chr4 585 0.94 chrX 605 1.93 All I want to do is re-scaling the third column to be between 0-1. Which means that the highest valu in 3rd column will... (5 Replies)
Discussion started by: @man
5 Replies
All times are GMT -4. The time now is 12:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy