2
$\begingroup$

I have an imported data set that I've used GroupBy on to be organized by specific elements of the sublists within the data. These keys are strings of numbers (in particular, they are grouped by day and then by the hour, which is #[[2, 3]], and #[[2, 4]] respectively). I am now trying to pick out various keys from these new associations. Here is what a piece of the array looks like:

{{string1}, {2016, 3, 4, 0, 0}}

So, if I wanted to examine every piece of information from the 4th day of March, I could do Key["4"], which yields:

<|"0" -> {{string1}, {2016, 3, 4, 0, 0}} |>

I can pick them out individually like this to look at with just the Key function, but what I would like to do is pick out several keys to examine a range of days. I've tried using Keys, but it doesn't seem to take just a key argument, and going through the whole data set to grab the explicit key values isn't a viable option. Is there an easier way to pick out multiple keys?

$\endgroup$
  • 2
    $\begingroup$ Check Lookup in the documentation. $\endgroup$ – István Zachar Jul 13 '16 at 15:53
  • $\begingroup$ Thanks, this function is very useful; I am still trying to get a hang of all these new functions introduced in MMa 10. I have some thoughts about Lookup, which I'll put as an answer. It might help someone else $\endgroup$ – Lame-Ov2.0 Jul 13 '16 at 16:30
3
$\begingroup$

At István's suggestion, I checked out the Lookup function, and was able to find a solution:

Lookup[#, ToString/@Range[10], Nothing[]]&

This takes the association that was previously created by GroupBy, and returns back the keys at the first level (which are strings corresponding to the days of the month) 1-10. Any key that doesn't return a value is dropped. My only qualm about this is that it doesn't return the actual keys, just the key values; from an organizational standpoint, this makes things a bit messier, but it's something I can live without for now.

Lookup is very robust, and it would be easy to apply it to multiple levels if you have a nested association.

$\endgroup$
2
$\begingroup$

Regarding Lookup you wrote:

My only qualm about this is that it doesn't return the actual keys, just the key values; from an organizational standpoint, this makes things a bit messier, but it's something I can live without for now.

I believe you want KeyTake. Recreating your situation:

SeedRandom[1]

dat = {RandomChoice[List /@ {"a", "b", "c"}, 15], 
    RandomInteger[4, {15, 5}]}\[Transpose];

asc = GroupBy[dat, Extract[{2, 3}]];

KeyTake[asc, Range[2, 5]]
<|2 -> {{{"b"}, {4, 0, 2, 4, 0}}, {{"a"}, {1, 0, 2, 0, 3}}, {{"b"}, {0, 4, 2, 3, 
     1}}, {{"b"}, {3, 1, 2, 0, 4}}}, 
 3 -> {{{"a"}, {0, 2, 3, 3, 2}}, {{"c"}, {2, 4, 3, 0, 3}}, {{"a"}, {2, 0, 3, 1, 
     2}}},
 4 -> {{{"a"}, {0, 0, 4, 2, 2}}, {{"a"}, {2, 3, 4, 3, 0}}, {{"a"}, {1, 3, 
     4, 4, 2}}, {{"a"}, {0, 3, 4, 1, 3}}, {{"a"}, {3, 4, 4, 2, 0}}, {{"b"}, {4, 4, 
     4, 4, 1}}}|>

Note that key 5 does not exist in the data so it is simply not returned; there is no need to use Nothing to remove it.

$\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.