JSL tip: finding unique values in a list

One of the great things about attending the JMP Discovery Summit as a developer is hearing questions and suggestions from people with so many different applications of JMP. One JMP Scripting Language (JSL) question came up three times, so it's worth sharing. The question is, "How do I remove duplicate items from a list?"

That is, given the list {"like", "dislike", "like", "neutral", "dislike", "like"} the result should be {"dislike", "like", "neutral"} (order not important).

There are probably lots of reasonable ways to do that in JSL (other ideas are welcome), but the simplest I could think of is to use an associative array. An associative array is a collection of key-value pairs where the keys are unique. If you add more than one key-value pair with the same key, only one of them is kept (the last one). If you don't care about the values, then the associative array is effectively a set of unique keys.

In JSL, you can create an associative array in several ways, but the only one of interest here is to create it from a list. The resulting associative array will treat the list values as keys and only store the unique keys. Then we can ask the associative array for a list of its keys.

answers = {"like", "dislike", "like", "neutral", "dislike", "like"};
answers set = Associative Array( answers );
unique answers = answers set << Get Keys;

Result: {"dislike", "like", "neutral"}

If you haven't discovered associative arrays in JMP, explore the docs and the examples in the JSL Scripting Index.

Now, it's your turn:
What other ways can you get unique values from a list? I can think of one using the Summarize() function...

tags: Discovery Summit, JMP - General, JMP Scripting Language, Tips and Tricks

7 Comments

  1. Michael Crotty
    Posted October 18, 2011 at 10:18 am | Permalink

    Another built-in JSL function that can help with this problem is the Design() function:

    answers = {"like", "dislike", "like", "neutral", "dislike", "like"};
    design(answers, < < Levels)[2]

  2. Guillaume
    Posted October 19, 2011 at 3:15 am | Permalink

    Another way is to use loop statement and Loc() function to find unique values

    answers = {"like", "dislike", "like", "neutral", "dislike", "like"};
    unique_answers = List();
    For (i=1,i<=Nitems(answers),i++,
    If (Nrows(Loc(unique_answers,answers[i])) == 0,
    InsertInto (unique_answers,answers[i])
    );
    );

  3. Pmroz
    Posted October 20, 2011 at 9:26 am | Permalink

    Finding unique values in a list came up
    in the JMP discussion forum a while back.  Here's the function
    that I use, which is based on the technique Xan showed.:

    /*

    Function Name: get_unique_values
    Description: Find the unique values in a list. 
    Uses an associative array
    Arguments:
    in_list     List to get unique values for

    */
    Get_unique_values = Function( {in_list}, {Default Local},
       tmp = [=> 0];
       Insert Into( tmp, in_list );
       tmp << get keys;
    );

  4. Leighton Spadone
    Posted November 3, 2011 at 1:03 pm | Permalink

    //JSL Matrix functions used to reduce a List to Unique Values;

    //A List with duplicate values;
    answers = {"like", "dislike", "like", "neutral", "dislike", "like"};

    //Colapse a design matrix to single row with 1 for 1st occurrence and 0 for duplicates;
    DesignMatrix =VMax(design(answers, answers));
    //Result: DesignMatrix = [1 1 0 1 0 0];

    //Subset List of answers to unique values;
    UniqueAnswers=answers[Loc(DesignMatrix)];
    //Result: UniqueAnsers = {"dislike", "like", "neutral"};

    • James Cultra
      Posted June 27, 2014 at 5:52 pm | Permalink

      This method works better for matrices as the associative array function appears to round the values in the matrix (thus the unique list doesn't match the actual values in the matrix)

  5. Eric Wittry
    Posted December 2, 2011 at 5:37 pm | Permalink

    If I have a key=>value pair, and I want to sort the values and return the keys in that order (*based on sorted value), how would I do that in JMP. Thanks Eric

    • Xan Gregg Xan Gregg
      Posted December 5, 2011 at 10:23 am | Permalink

      If you iterate through an associative array (with the First and Next messages), the entries are returned in order of the sorted keys. If the values are unique, you can use them as keys, but if not, I think it will require explicit sorting. You might try the JMP Community Forum for ideas on the latter.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>