I use the following code:
i1 <- SRC2[, Co:= as.character(round(sum(Exist)/.N, 2)) ,
sequenceID][, .I[1:(.N-1)], sequenceID]$v1
SRC2[i1, Co:= '']
SRC2
I would to change the line code so only the last occurrence of the same values of sequenceID will contain the Co value and not all the lines as in this code.
The current output (each line has value "Co"): "
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
The required output (only the last line of the same "sequenceID" contains value "Co":
"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