仅计算列的相同值的最后一行的值

时间:2015-11-21 22:02:34

标签: r dataframe row

我使用以下代码:

    i1 <- SRC2[, Co:= as.character(round(sum(Exist)/.N, 2)) , 
               sequenceID][, .I[1:(.N-1)], sequenceID]$v1


    SRC2[i1, Co:= '']
    SRC2

我想更改行代码,因此只有sequenceID的相同值的最后一次出现将包含Co值,而不是此代码中的所有行。

当前输出(每行有值&#34; Co&#34;): &#34;

sequenceID" "transactionID" "eventID" "items"  "Exist" "Co"
"1" 42207993       1577          1        "OV50"     1     0.67
"2" 42207993       6048          2        "OV11"     0     0.67
"3" 42207993       1597          3        "OV148"    1     0.67
"4" 57237976       12423         1        "OV46"     0     0.5
"5" 57237976       12589         2        "OV197"    1     0.5

所需的输出(仅相同的最后一行&#34; sequenceID&#34;包含值&#34; Co&#34;:

"sequenceID" "transactionID" "eventID" "items"  "Exist" "Co"
"1" 42207993       1577          1        "OV50"     1
"2" 42207993       6048          2        "OV11"     0  
"3" 42207993       1597          3        "OV148"    1       0.67
"4" 57237976       12423         1        "OV46"     0
"5" 57237976       12589         2        "OV197"    1       0.5

0 个答案:

没有答案