Re: Select and Cases Give Different Answers
- To: mathgroup at smc.vnet.net
- Subject: [mg99160] Re: [mg99125] Select and Cases Give Different Answers
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Wed, 29 Apr 2009 03:48:00 -0400 (EDT)
- Reply-to: hanlonr at cox.net
theList = {"NA", "NA", -0.01315, 0.0120957, -1/41, 0.00625, "NA", "NA", 5/8, "NA"}; Unequal doesn't make any assumptions about a Rational # != "NA" & /@ theList {False, False, True, True, -(1/41) != "NA", True, False, False, 5/8 != "NA", False} You can force it with N f1 = Select[theList, N[#] != "NA" &] {-0.01315, 0.0120957, -(1/41), 0.00625, 5/8} Here are some other alternatives f1 == Cases[theList, _?(N[#] != "NA" &)] True fl == Cases[theList, _?NumericQ] True fl == Select[theList, NumericQ] True fl == DeleteCases[theList, "NA"] True fl == DeleteCases[theList, _?StringQ] True fl == DeleteCases[theList, _?(! NumericQ[#] &)] True fl == Select[theList, ! # === "NA" &] True fl == Cases[theList, _?(! # === "NA" &)] True fl == Select[theList, # =!= "NA" &] True fl == Cases[theList, _?(# =!= "NA" &)] True fl == Cases[theList, Except["NA"]] True fl == Cases[theList, Except[_?StringQ]] True fl == DeleteCases[theList, Except[_?NumericQ]] True fl == (theList /. "NA" :> Sequence[]) True fl == (theList /. _?StringQ :> Sequence[]) True fl == Last[GatherBy[theList, NumericQ]] True fl == Last[GatherBy[theList, StringQ]] True f1 == Pick[theList, NumericQ /@ theList] True f1 == Pick[theList, Not /@ StringQ /@ theList] True f1 == Pick[theList, (N[#] != "NA") & /@ theList] True f1 == Pick[theList, Thread[N[theList] != "NA"]] True Bob Hanlon ---- Gregory Lypny <gregory.lypny at videotron.ca> wrote: ============= Hello everyone, Suppose I have the following list that is a mixture of the string "NA" and five numeric quantities. theList = {"NA", "NA", -0.01315, 0.0120957, -1/41, 0.00625, "NA", "NA", 5/8, "NA"} I want to weed out the NAs, and all of the following Cases[theList, _?NumericQ] DeleteCases[theList, _?StringQ] Select[theList, # =E2=88=88 Reals &] Select[theList, # =!= "NA" &] work fine because each returns {-0.01315, 0.0120957, -(1/41), 0.00625, 5/8}. But I don't understand why Unequal, when used as the criterion for Select as in Select[theList, # != "NA" &], fails to return the rationals, and I get only three elements {-0.01315, 0.0120957, 0.00625}. I'd appreciate any clarification on the difference between UnsameQ and Unequal because my inclination in most circumstances is to use an equal sign or an unequal sign to make simple comparisons yet it doesn't give me the answer I expect, and I'd hate to make a costly mistake somewhere down the road! Regards, Gregory