Imagine that I have a list of numbers (i.e. numbers column in data.table/data.frame).
1
5
5
10
11
12
for each number in a list a want to count how many unique numbers are there which are lower than that particular number + 5.
The explanation for upper case, first number = 1, search range is 1+5 = 6, so three numbers are in range, less than or equal to: c(1,5,5)
, and then count unique is 2.
This is all by assuming we've got the additional condition, that the number must not only be lower than current_number + 5, but also its index in the list must be >= that of current_number.
The result in this case would be:
2
2
2
3
2
1
Note: Is there a fast solution for huge dataset, in data.frame or data.table? My dataset is rather huge, 10+M rows.
1
) the comparison value is6
. 3 unique values in the vector are higher than 6, but your result is2
. Where do I misunderstand you? – Candelaria2, 2, 2, 5, 5, 5
?...Also I think you mean less than..? because1, 5, 5
are less than 6..? I don't know. I m confused – Inkster{1, 5, 5, 10, 11, 12}
, and the number of unique numbers in that set is 5. Why is your desired result 2? Perhaps, the search range is[x, x+5]
rather than(-Inf, x+5]
? Also, please clarify by "lower than" if you mean<
or<=
. – Wholesome<
(or maybe<=
)x + 5
? – Wholesome