indcv

Purpose

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

Format

z = indcv(needle, haystack)
Parameters:
  • needle (Nx1 character vector) – contains the elements to be found in vector haystack

  • haystack (Mx1 character vector) – searched for matches to the elements of needle

Returns:

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

Examples

newVars = { YEARS, BONUS, GENDER };
needle = { AGE, PAY, SEX };
haystack = { AGE, SEX, JOB, DATE, PAY };

// Return the indices in 'haystack' of the items in 'needle'
z = indcv(needle, haystack);

// Replace AGE, PAY, SEX with YEARS, BONUS, GENDER
haystack[z] = newVars;

After the code above:

        YEARS
       GENDER       1
haystack =   JOB   z = 5
         DATE       2
        BONUS

Remarks

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

Both arguments will be forced to uppercase before the comparison.

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

See also

Functions indnv(), indsav()