Formula to check if any of a list of items is contained in the rows of a column
A week ago(448 views)
JMP for Mac
Version 17.2.0 (701896)
I have a column "Data" with rows of number_number like this:
1_3
3_7
6_5
...
I want to test this column to see if some of the rows contain values from a list of values and then flag or label the rows which do.
I know I could construct an elaborate formula like this:
If( :Die X_Y == "3_9" | :Die X_Y == "3_4",
1,
0
)
But that's very laborious for long lists of different values to check. I'd like to just be able to paste a list of values to check for in a formula, delimited by a space or comma.
I found the function "Contains Item." It works fine for one item but the help entry doesn't offer any insight on how to construct the "list" option. I tried this (specifying the optional delimiter as a space):
I understand the default delimiter is a comma but wanted to enforce to find anything that works. Neither Contains Item function is returning the sought-for behavior (logical 1 if there is a match, 0 if not). If there is a better function to use please advise.
This is a generalized problem I'd like to be able to solve -- inspecting a column to find matches with long lists of items.
Usually in cases like this I tend to use Words() to split the string into a list and then compare using Contains() (or in some rare cases Contains Item()). But Contains Item() should also work and you just seem to have your parameters in incorrect order
New Table("Untitled",
Add Rows(3),
Compress File When Saved(1),
New Column("Column 1", Character, "Nominal", Set Values({"1_3", "3_7", "6_5"})),
New Column("Column 2",
Character,
"Nominal",
Formula(
If(Contains Item("1_3 3_9", :Column 1, " "),
"exclude",
"not exclude"
)
),
Set Selected
)
)
Usually in cases like this I tend to use Words() to split the string into a list and then compare using Contains() (or in some rare cases Contains Item()). But Contains Item() should also work and you just seem to have your parameters in incorrect order
New Table("Untitled",
Add Rows(3),
Compress File When Saved(1),
New Column("Column 1", Character, "Nominal", Set Values({"1_3", "3_7", "6_5"})),
New Column("Column 2",
Character,
"Nominal",
Formula(
If(Contains Item("1_3 3_9", :Column 1, " "),
"exclude",
"not exclude"
)
),
Set Selected
)
)
I did some timing tests comparing Jarmo's solution compared to my solution, and the Contains Item solution is spectacularly faster than the Select Where solution.
In the beginning I had quite bad experience with Where() - but cannot remember why. Maybe I used a wrong syntax - or the issue got fixed in the mean time ...
Jarmo found my problem -- switching the list and item in my formula. Thanks for the bit of code illustrating the correct usage.
re: "Documentation a bit lacking" I concur. The instructions and example in the scripting index do seem a little ambiguous to me. Here is the entry for the scripting index:
Returns a Boolean that indicates whether the word (item), one of a list of words (list), or pattern (pattern) matches one of the words in the text represented by x. Words are delimited by the characters in the optional quoted delimiter (delimiter) string. A comma is the default delimiter. Blanks are trimmed from the ends of each extracted word from the input text string (x).
"
So (to my simplistic reading) either x or the {list} could contain the words to be searched or the list of words to compare it with. A few more examples showing some alternatives that included a Column as in Jarmo's example might help clarify.
I've put the solution to use already! Thanks again.