2
$\begingroup$

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".

$\endgroup$
1
  • $\begingroup$ Look at Select. $\endgroup$
    – bill s
    Commented Dec 18, 2014 at 16:02

3 Answers 3

1
$\begingroup$

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
$\endgroup$
1
$\begingroup$

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 *)
cityHeader = {"City", "State", "Population"};
(* also create an association *)
cityAssoc = 
Flatten[AssociationThread[cityHeader -> #] & /@ cityList];

Solution:

(* grab anything that has the 2nd element of the list equal to NewYork *)
Pick[#, (Thread@#)[[2]], "NewYork"] &@cityList

Pick Result

Using Select instead

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

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

Excerpt of result - a long list

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"]]]  

note the or clauses bring in LA and other big cities

Will also work on an association

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

enter image description here

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

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

enter image description here

$\endgroup$
0
$\begingroup$

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"], _}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.