movingTopNIndex(X, window, top, [ascending=true], [fixed=false], [tiesMethod='oldest'])


X is a numeric/temporal vector. Note: Starting from version 2.00.10, the NULL values in X do not participate in data sorting.

window is an integer no less than 2, indicating the window size.

top is an integer greater than 1 and no greater than window.

ascending is a Boolean value indicating whether the data within a window is sorted in ascending order. True means ascending order and false means descending order.

fixed is a Boolean value, indicating whether the length of each row in the output array vector is fixed to be top. The default value is false. When fixed = true, all rows are of the same length. For the first (top - 1) windows, the indices of missing elements are replaced with NULL.

tiesMethod is a string that specifies how to select elements if there are more elements with the same value than spots available in the top N after sorting X within a sliding window. It can be:
  • 'oldest': select elements starting from the earliest entry into the window;

  • 'latest': select elements starting from the latest entry into the window.


Return an array vector indicating the indices of the first top elements of X after sorted within each sliding window.


S = 2 5 6 1 2 4 5 6 9 0
m1 = movingTopNIndex(X=S, window=4, top=2, ascending=true, fixed=true)
// output

m2 = movingTopNIndex(X=S, window=4, top=2, ascending=false, fixed=true)
// output

m3 = movingTopNIndex(X=S, window=4, top=2, ascending=true, fixed=false)
print m3;
// output

// output

// output
X = [5, 8, 1, 9, 7, 3, 1, NULL, 0, 8, 7, 7]
movingTopNIndex(X=X, window=4, top=2, ascending=true, fixed=true)
// Before version 2.00.10, the NULL values in X are involved in data sorting.
// output
// As of version 2.00.10, the NULL values in X are ignored in data sorting.
// output

X = [2, 1, 4, 3, 4, 3, 4]
// For the sixth window, the sorted X is 1 2 3 3 4 4.
// As tiesMethod is not specified, the default 'oldest' is used, meaning the first occurrence of 3 (at index 3) is selected.
// output

// As tiesMethod is set to 'latest', the latest occurrence of 3 (at index 5) is selected.
// output