如果列匹配则计算值

时间:2017-09-07 00:00:24

标签: r bash

我在df中有两行,如下所示:

chr1    4771959 4772759     1   AJAP1
chr1    4771959 4772759     3   AJAP1
chr1    6291961 6292179     1   ICMT
chr1    6291961 8421822     3   ICMT

我想比较前三列,如果相同,则组合并添加第四列的值。我想要的输出是这样的:

chr1    4771959 4772759     4   AJAP1
chr1    6291961 6292179     1   ICMT
chr1    6291961 8421822     3   ICMT

这是否可以在bash中实现,或者如果更容易,可以在R ??

中实现

3 个答案:

答案 0 :(得分:3)

在R中,您可以使用aggregate(V4~., df, sum) # V1 V2 V3 V5 V4 #1 chr1 4771959 4772759 AJAP1 4 #2 chr1 6291961 6292179 ICMT 1 #3 chr1 6291961 8421822 ICMT 3

df = structure(list(V1 = c("chr1", "chr1", "chr1", "chr1"), V2 = c(4771959L, 
4771959L, 6291961L, 6291961L), V3 = c(4772759L, 4772759L, 6292179L, 
8421822L), V4 = c(1L, 3L, 1L, 3L), V5 = c("AJAP1", "AJAP1", "ICMT", 
"ICMT")), .Names = c("V1", "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, 
-4L))

DATA

{{1}}

答案 1 :(得分:2)

在bash中你使用awk:

$ awk '
{
    a[$1 OFS $2 OFS $3]+=$4
    b[$1 OFS $2 OFS $3]=$5
}
END {
    for(i in a)
        print i,a[i],b[i]
}' file
chr1 6291961 8421822 3 ICMT
chr1 6291961 6292179 1 ICMT
chr1 4771959 4772759 4 AJAP1

答案 2 :(得分:0)

在bash我会说:

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import * as rsx from 'rxjs'

class Test {
    // Dummy service decalration
    _service : {
        getOptionsQuestion(nr: number): rsx.Observable<Array<{Content : string }>>
    }

    result: string;
    async getAllOptions(question_ID: number){

        this.result = "";
        const data = await this._service.getOptionsQuestion(question_ID).toPromise()

        data.forEach(item => {
            console.log(item.Content);
            this.result += item.Content;
        });
    }
    async otherMethod (questionID : number){
        await this.getAllOptions(questionID);
        console.log("++++++++++++++++++++++++++++++++");
        console.log(this.result);
    }
}