0

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

1 Answer 1

1

Here is the required code:

library(data.table)
setDT(SRC2)

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

SRC2[i1, Co:= '']
SRC2

The last part of i1 has to contain V1 (in Capital letter)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.