Re: Using Select
- To: mathgroup at smc.vnet.net
- Subject: [mg76227] Re: [mg76162] Using Select
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Fri, 18 May 2007 06:08:44 -0400 (EDT)
- References: <200705170954.FAA00930@smc.vnet.net>
Mark Coleman wrote: > Greetings, > > I'm working on a small application and I'm searching for a way to > Select a subset of rows from a list based upon a list of criteria > chosen by the user. Here is small (hypothetical) example. Say one > has a data set of consisting of a list of as follows: > > { {1,A,Blue,"Hello",10.5},{7,D,Green,"Goodbye",9.4}, > {6,S,Yellow,"Hello",6.9},{3,A,Blue,"Hello",8.0}....} > > The user will specify a letter and a color and the program should > Select[ ] the appropriate rows, e.g., if I pick Color=Blue and Letter > = A, then it will return > > { {1,A,Blue,"Hello",10.5},{3,A,Blue,"Hello",8.0}....}, etc. Thus one > can enter Select[myData,(#[[2]]==A && #[[3]]==Blue)&] and get the > appropriate records.Note that The actual data set has about 20,000 > records (lists), each with about 20 fields, and can select 7 or 8 > different attributes, so the Select statements get fairly long. > > My question involves an efficient way to code the Select statement if > a user wants to chose records that correspond to any value of a > particular field, e.g., All of the Color=Blue, regardless of the > Letter choice,e.g., > > Select[myData,(#[[3]]==Blue)&] > > More precisely, is there a way to pass Select the user choice of any > letter using some sort of 'wildcard' value that represents any value > for the specific attribute, i.e., > > Select[myData,(#[[2]]=='Wildcard' && #[[3]]==Blue)&] > > My motivation is that if I have may possible selection criteria, I > can still use a single Select statement. > > Thanks, > > -Mark > You could use MatchQ in the second argument to Select. The pattern to match will be a list of length equal to the number of columns in your data and will have Blank[]s for each attribute without a specified value. In the example given with data representing your data set, Select[data, MatchQ[#, {_, A, Blue, _, _}] &] will select all rows where the second element is A and the third is Blue, while Select[data, MatchQ[#, {_, _, Blue, _, _}] &] will select all rows with Blue as the third element. Darren Glosemeyer Wolfram Research
- References:
- Using Select
- From: Mark Coleman <mark@markscoleman.com>
- Using Select