# Import Data using selection criteria

I imported a file (text and numbers) and made constructed this. e[[1,All,{1,2,3,4,6}]] Column 1 is the name of the state. How can I get a subset of this where, say Column 1 is not equal to "New York", or Column 1 equals "California".

• Look at Select. Commented Dec 18, 2014 at 16:02

Your question is not clear. Assuming I understood right. You can try this.

states = Table[Subscript[state, i] , {i, 1, 50}];
dat = RandomReal[{1, 5}, {50, 3}];
mergeddat = Transpose[Prepend[Transpose[dat], states]];
key = Table[Subscript[state, i] , {i, 1, 50, 10}];
mydata =
Flatten[Table[
Select[mergeddat, #[[1]] == key[[i]] &], {i, 1, Length[key]}],
1] // MatrixForm


An alternate method to select

   mydata= Select[mergeddat, MemberQ[key, #[[1]]] &] // MatrixForm


Assuming you want to to filter your original list by strings - something that is fairly lightly covered in the MMA help imo - you can do this. Note that the usage of Entity and Quantity are a result of using the CityData and not essential for these examples.

There are a number of good threads on Stack Exchange covering this - here and here for example.

Preparation

(* Create a 3 part list consisting of city names regions and
population, also create some column headings and an association for
later *)
cityList = {#, CityData[#, "Region"],
CityData[#, "Population"]} & /@ CityData[{Large, "UnitedStates"}];
(* also create some column headings *)
(* also create an association *)
cityAssoc =


Solution:

(* grab anything that has the 2nd element of the list equal to NewYork *)


(* Similar code using select and not equals (esc != esc)*)

Select[cityList, Function[{x}, x[[2]] != "NewYork"]]


Extend with a pair of Or clauses to include LA (as a Wolfram Language Entity) and anywhere with a population > 2Mn people as a Quantity

Select[cityList, Function[{x}, x[[2]] == "NewYork" ||
x[[1]] == Entity["City", {"LosAngeles", "California", "UnitedStates"}] ||
x[[3]] > Quantity[2000000, "People"]]]


Will also work on an association

Select[cityAssoc , Function[{x}, x[[3]] > Quantity[2000000, "People"]]]


But we can create a more readable version using the association keys (#xxxx)

Select[#State == "NewYork" || #Population > Quantity[2000000, "People"] &]@cityAssoc


An option if you haven't got Mathematica 10 is using Cases

cityList = {#, CityData[#, "Region"], CityData[#, "Population"]} & /@
CityData[{Large, "UnitedStates"}];
Cases[cityList, {_, a_ /; ! StringMatchQ[a, "NewYork"], _}]