Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206235

How to divide specific column with rest of columns

$
0
0

I have matrix like this (first column names rest are values, separator i tab):

name1 A1 B1 C1 D1
name2 A2 B2 C2 D2

Matrix could be huge (it is mean about hundreds rows and columns). It is allays same size. I can expect zero values.

I need output like this:

name1 A1 B1 C1 D1 A1/B1 A1/C1 A1/D1
name2 A2 B2 C2 D2 A2/B2 A2/C2 A2/D2

This combination save to new file. And then make another combination:

name1 A1 B1 C1 D1 B1/A1 B1/C1 B1/D1
name2 A2 B2 C2 D2 B2/A2 B2/C2 B2/D2

and so on so on => divide each column with rest of columns in matrix and save as TSV to new file. And also round to three decimal places.

I can do this manually with script:

awk '{OFS="\t"}{$6=$2/($3+0.001); $7=$2/($4+0.001); $8=$2/($5+0.001)}1' input_file.tsv

Reason why I add number 0.001 is that division by zero is impossible. I can create shell script with wile loop, but it takes long time.

I would be very happy for any automation this process.


Viewing all articles
Browse latest Browse all 206235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>