4
$\begingroup$

In the following Dataset, I want to use Select to display only rows, which contain numbers in column c that are even. These elements don't all have to be even, but at least one in each row. Basically so that the only rows that are removed are the ones that have only odd numbers in c.

dataq = Dataset[{<|"a" -> 13, "b" -> "z", "c" -> {1, 8, 6, 9}|>, <|
"a" -> 24, "b" -> "y", "c" -> {2, 3}|>, <|"a" -> 4, "b" -> "d", 
"c" -> {4, 5}|>, <|"a" -> 32, "b" -> "y", "c" -> {3, 8, 9}|>, <|
"a" -> 8, "b" -> "n", "c" -> {5, 6, 7}|>, <|"a" -> 19, "b" -> "x",
 "c" -> {1}|>}]

The farthest I've gotten is with MemberQ, but how do you condition that on an EvenQ condition instead of a number:

dataq=[Select[MemberQ[#c, 5] &]]

Thanks guys!

$\endgroup$
  • $\begingroup$ Sticking with your origination approach this does the job for you: dataq[Select[MemberQ[#c, _?EvenQ] &]] $\endgroup$ – Mike Honeychurch Apr 4 '16 at 19:28
5
$\begingroup$
    dataq[Select[AnyTrue[#c, EvenQ] &]]
$\endgroup$
  • $\begingroup$ Beat me to it, I was going to post Select[dataq, (Or @@ EvenQ[#c]) &] - slightly different but mostly the same $\endgroup$ – Jason B. Mar 14 '16 at 11:07
3
$\begingroup$

I'm not sure if that's the way to go in general but in 10.4 KeyValuePattern was introduced:

DeleteCases[
 dataq,
 KeyValuePattern["c" -> {__?OddQ}]
]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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