使用特定patters选择列然后找到总和的比例 [英] Selecting columns using specific patters then finding sum and ratio

查看:158
本文介绍了使用特定patters选择列然后找到总和的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算从下面的数据之和的比例值。 (实际数据包含超过20万列和行45000(系))。

I want to calculate the sum and ratio values from data below. (The actual data contains more than 200,000 columns and 45000 rows (lines)).

为了清楚目的,我给了只有简单的数据格式。

For clarity purpose I have given only simple data format.

#Frame  BMR_42@O22  BMR_49@O13  BMR_59@O13  BMR_23@O26  BMR_10@O13  BMR_61@O26  BMR_23@O25 
 1      1           1           0           1           1           1           1
 2      0           1           0           0           1           1           0
 3      1           1           1           0           0           1           1
 4      1           1           0           0           1           0           1
 5      0           0           0           0           0           0           0
 6      1           0           1           1           0           1           0
 7      1           1           1           1           0           0           0
 8      1           1           1           0           0           0           0
 9      1           1           1           1           1           1           1
10      0           0           0           0           0           0           0

的列需要用一定的标准来选择。

The columns need to be selected with certain criteria.

这是我考虑的列数据仅与列 @ O13 。下面我从上面的例子给出的选定列。

The column data which I consider is columns with "@O13" only. Below I have given the selected columns from above example.

BMR_49@O13  BMR_59@O13  BMR_10@O13  
1           0           1       
1           0           1       
1           1           0       
1           0           1       
0           0           0       
0           1           0       
1           1           0       
1           1           0       
1           1           1       
0           0           0   

从选定的专栏中,我想计算:

From the selected column, I want to calculate:

1)中的所有1的总和。在这个例子中,我们得到值16。

1) the sum of all the "1"s. In this example we get value 16.

2)含有的1(至少一次)发生总的行数。从上面的例子有含有的至少一种发生8行1。

2) the number of total rows containing occurrence of "1" (at least once). From above example there are 8 rows which contain at least one occurrence of "1".

最后,

3)总所有的1,总线,1的发生的比率。

3) the ratio of total of all "1"s with total lines with occurrence of "1"s.

也就是说::(所有1的)/(具有1的次数总行)。
例如16/8

That is :: (total of all "1"s)/(total rows with the occurance of "1"). Example 16/8

作为开始,我试图用这个命令只选择列 @ O13

As a start, I tried with this command to select only the columns with "@O13"

awk '{for (i=1;i<=NF;i++) if (i~/@O13/); print ""}' $file2

虽然这跑,但不显示的值。

Although this run but doesn't show up the values.

如果任何形式的帮助是提供鸭preciate。

Appreciate if any kind help is offered.

推荐答案

这应该做的:

awk 'NR==1{for (i=1;i<=NF;i++) if ($i~/@O13/) a[i];next} {f=0;for (i in a) if ($i) {s++;f++};if (f) r++} END {print "number of 1="s"\nrows with 1="r"\nratio="s/r}' file
number of 1=16
rows with 1=8
ratio=2

一些更具可读性:

Some more readable:

awk '
NR==1{
    for (i=1;i<=NF;i++) 
        if ($i~/@O13/)
            a[i]
        next
    }
    {
    f=0
    for (i in a)
        if ($i=="1") {
            s++
            f++
        }
    if (f) r++
    } 
END {
    print   "number of 1="s \
            "\nrows with 1="r \
            "\nratio="s/r
    }
' file

这篇关于使用特定patters选择列然后找到总和的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆