我编写了一个代码来计算zscore,它计算一个文件的平均值和标准偏差,并使用另一个文件中行的一些值,如下所示:
mean=$(awk '{total += $2; count++} END {print total/count}' ABC_avg.txt)
#calculating mean of the second column of the file
std=$(awk '{x[NR]=$2; s+=$2; n++} END{a=s/n; for (i in x){ss += (x[i]-a)^2} sd = sqrt(ss/n); print sd}' ABC_avg.txt)
#calculating standard deviation from the second column of the same file
awk '{if (std) print $2-$mean/$std}' ABC_splicedavg.txt" > ABC.tmp
#calculate the zscore for each row and store it in a temporary file
zscore=$(awk '{total += $0; count++} END {if (count) print total/count}' ABC.tmp)
#calculate an average of all the zscores in the rows and store it in a variable
echo $motif" "$zscore
rm ABC.tmp
但是,当我执行此代码时,在创建临时文件的步骤中,我收到错误致命:尝试除零,实现此代码的正确方法是什么? TIA我使用了bc -l选项,但是它提供了一个非常长的浮动整数版本。
答案 0 :(得分:1)
这是一个在一次通过中计算均值和标准的脚本,如果不能接受,你可能会失去一些解决方案...
$ awk '{print rand()}' <(seq 100)
| awk '{sum+=$1; sqsum+=$1^2}
END{print mean=sum/NR, std=sqrt(sqsum/NR-mean^2), z=mean/std}'
0.486904 0.321789 1.51312
每个样本的z-score脚本都是错误的!你需要做($ 2-mean)/ std。
答案 1 :(得分:1)
您可以使用scale
变量
$ echo "4/7" | bc -l
.57142857142857142857
$ echo "scale=3; 4/7" | bc -l
.571