indnv

Purpose

Checks one numeric vector against another and returns the indices of the elements of the first vector in the second vector.

Format

z = indnv(needle, haystack[, sorted])
Parameters:
  • needle (Nx1 numeric vector) – contains the values to be found in vector haystack.

  • haystack (Mx1 numeric vector or dataframe) – searched for matches to the values in needle

  • sorted – Optional. Indicates if the input data needle and haystack are both sorted. If a 1 is provided to indicate the data is already sorted, an optimized algorithm is used internally to perform the matching. Default = 0.

Returns:

z (Nx1 vector of integers) – the indices of the corresponding elements of needle in haystack.

Examples

Basic Usage

// What elements to look for
needle = { 8, 7, 3 };

// Vector to look in
haystack = { 2, 7, 8, 4, 3 };

// Find locations of needle in haystack
z = indnv(needle, haystack);
    3
z = 2
    5

Presorted data

// What elements to look for
needle = { 3, 8 };

// Vector to look in
haystack = { 2, 3, 4, 7, 8 };

// Find locations of needle in haystack
z = indnv(needle, haystack, 1);
    2
z = 5

Remarks

If no matches are found for any of the elements in needle, then those elements in the returned vector are set to the GAUSS missing value code.

If there are duplicate elements in haystack, the index of the first match will be returned.

If needle and haystack are both dataframes and are both string/categorical, they must both originate from the same column. If this criteria is not met, use indsav() instead.

See also

Functions contains(), ismember(), rowcontains()