Add a number all the the values and log transform them using awk

Hi, is it possible to add a number to all the values in a file and log transform them using awk. thanx in advance

input

name    c1      c2      c3      c4
r1      0       0.2     0.3     0.4
r2      0       0       0       1
r3      1       0       0       0
r4      0       0       1       1
r5      32      23      11      11

add 1 to every value

name    c1      c2      c3      c4
r1      1       1.2     1.3     1.4
r2      1       1       1       2
r3      2       1       1       1
r4      33      24      12      12

log2 transform every value

name    c1      c2      c3      c4
r1      log2(1) log2(1.2)       log2(1.3)       log2(1.4)
r2      log2(1) log2(1) log2(1) log2(2)
r3      log2(2) log2(1) log2(1) log2(1)
r4      log2(33)        log2(24)        log2(12)        log2(12)

divided every value by the sum of that specific row

name    c1      c2      c3      c4
r1      log2(1)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4)   log2(1.2)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4) log2(1.3)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4) log2(1.4)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4)
r2      log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(2)/log2(1)+log2(1)+log2(1)+log2(2)
r3      ...
r4      ..

Not the slightest attempt from your side?
Try

awk     'NR==1          {ln2=log(2); print; next}
                        {sum=0
                         for (i=2; i<=NF; i++)
                                {$i=log($i+1)/ln2
                                 sum+=$i}
                         for (i=2; i<=NF; i++)
                                $i/=sum
                        }
         1
        ' OFS="\t" CONVFMT="%.4g" file
name    c1      c2      c3      c4
r1      0       0.2334  0.3359  0.4307
r2      0       0       0       1
r3      1       0       0       0
r4      0       0       0.5     0.5
r5      0.3003  0.2729  0.2134  0.2134
1 Like