Re: Selecting Rows Based on Column Values
- To: mathgroup at smc.vnet.net
- Subject: [mg101540] Re: [mg101471] Selecting Rows Based on Column Values
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 9 Jul 2009 02:00:25 -0400 (EDT)
- Reply-to: hanlonr at cox.net
Select[tester, #[[3]] == 1 &] Cases[tester, _?(#[[3]] == 1 &)] Select[tester, Last[#] == 1 &] Cases[tester, _?(Last[#] == 1 &)] Cases[tester, {_, _, 1}] DeleteCases[tester, {_, _, Except[1]}] tester /. {_, _, Except[1]} -> Sequence[] Bob Hanlon ---- cynthia wu <wu.x.cynthia at gmail.com> wrote: ============= I've been trying to use the Select Command to select only the rows that have the a specific value in a specific column. I have, for example, the following matrix (4x3): tester={{1, 3, 1}, {3, 1, 0}, {4, 2, 1}, {5, 6, 2}} I want to select the rows that have values ==1 in the 3rd column. I've tried: select(tester, #[[All,3]]==1&), but I get an error saying that my request violates the matrix boundaries. The desired output should be: {{1,3,1}, {4,2,1}}. Any thoughts would be greatly appreciated. Thanks!