# Perform a conditional calculation on every row value, in a single col, with the all the row vals after current in a data.table

Hello,

I appreciate that the title might be confusing so I will try to explain more here.

So, consider the following `data.table`:

``````sampleDT <- data.table(id = c(1,2,3,4,5),
dist = c(111.0200,124.0074,124.1337,128.0353,132.0303),
score = c(2985048,21641962,2627225,10382804,4338643)
)
``````

I would like to subtract all the distances (column: `dist`) with the next ones in the array and make a conditional check for the resulted value. Pseudocode below to demo the logic:

``````if(dist[1]-dist[2] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[1]-dist[3] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[1]-dist[4] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[1]-dist[5] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[2]-dist[3] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[2]-dist[4] == xval) then sampleDT[, point := "Keep"] else continue,
if(dist[2]-dist[5] == xval) then sampleDT[, point := "Keep"] else continue,
.
.
.
if(dist[4]-dist[5] == xval) then sampleDT[, point := "Keep"] else continue
``````

Visual representation of the hops I want to be performed.

Is something like this feasible?

I'm not sure what you expect the output to look like, but you can use `shift()`:
Fast lead/lag for vectors and lists — shift • data.table (rdatatable.gitlab.io)

Thanks, I will have a look.

In terms of output, I'd expect to get a DT like the one below.

So, upon a conditional check on the subtracted value, the current row would be tagged as "keep" or NA

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.