Message Boards Message Boards

0
|
3116 Views
|
8 Replies
|
0 Total Likes
View groups...
Share
Share this post:

GatherBy Dateobject instance

How can I gather instances of DateObject like '{Month:Sep 2018}' into sublist by 'First'? I tried

Flatten[GatherBy[{2018,9}],First]

but it shows error.

POSTED BY: Shivam Sawarn
8 Replies

I am using it in solving an exercise and it is still not working. I added DateObject Flatten[GatherBy[DateObject[{2018,9}],First]

POSTED BY: Shivam Sawarn
Posted 2 years ago

If you are dealing with DateObject then

GatherBy[
 {DateObject[{2018, 9}], DateObject[{2019, 9}], 
  DateObject[{2019, 10}], DateObject[{2020, 9}]},
 DateValue[#, "Year"] &]

(*
{{DateObject[{2018, 9}, "Month"]}, {DateObject[{2019, 9}, "Month"], 
  DateObject[{2019, 10}, "Month"]}, {DateObject[{2020, 9}, "Month"]}}
*)

Don't understand why you are flattening the result, doing so will undo the GatherBy.

POSTED BY: Rohit Namjoshi

This is what I expect.!

![with flatten I get the same, but exercise does not pass it.][2]

POSTED BY: Shivam Sawarn
Posted 2 years ago

Please post code that can be copied/pasted, not images. The function needs to be DateValue[#, "Year"] &, not "Year". See the code in my answer.

POSTED BY: Rohit Namjoshi
GatherBy[{DateObject[{2015, 9}], DateObject[{2015, 1}], 
  DateObject[{2015, 5}], DateObject[{2017, 8}], DateObject[{2017, 5}],
   DateObject[{2017, 8}], DateObject[{2018, 1}], 
  DateObject[{2018, 8}], DateObject[{2018, 3}], DateObject[{2018, 2}],
   DateObject[{2018, 4}], DateObject[{2018, 9}], 
  DateObject[{2015, 6}], DateObject[{2015, 10}], 
  DateObject[{2015, 6}]}, "Year"] 

and

GatherBy[{DateObject[{2015, 9}], DateObject[{2015, 1}], 
  DateObject[{2015, 5}], DateObject[{2017, 8}], DateObject[{2017, 5}],
   DateObject[{2017, 8}], DateObject[{2018, 1}], 
  DateObject[{2018, 8}], DateObject[{2018, 3}], DateObject[{2018, 2}],
   DateObject[{2018, 4}], DateObject[{2018, 9}], 
  DateObject[{2015, 6}], DateObject[{2015, 10}], 
  DateObject[{2015, 6}]}, DateValue[#, "Year"] &]

yield same result and exercise doesn't pass any

POSTED BY: Shivam Sawarn
Posted 2 years ago

GatherBy calls the specified function for each element in the list. All elements for which the function gives the same result are put in a sublist. So, the key is to write the function so it returns the value that should be grouped. If the elements in the list are DateObject and you want to group by year then the function should return the year part of the DateObject, the best way to do that is to use DateValue to extract the year.

Alternatively, you can extract the year from the FullForm of DateObject.

DateObject[{2015, 9}] // First
(* {2015, 9} *)

DateObject[{2015, 9}] // First // First
(* 2015 *)

So you could give GatherBy the function First /* First and the result would be the same. Or to gather by month First /* Last.

If you just have a list of {year, month} then you can use the function First.

GatherBy[{{2015, 9}, {2015, 1}, {2015, 5}, {2017, 8}, {2017, 5},
 {2017, 8}, {2018, 1}, {2018, 8}, {2018, 3}, {2018, 2}, {2018, 4}, 
 {2018, 9}, {2015, 6}, {2015, 10}, {2015, 6}}, First]
POSTED BY: Rohit Namjoshi

Thanks Rohit. I found the error. I am curious to know how do you approach to such problem. I didn't come near vicinity to it

POSTED BY: Shivam Sawarn
Posted 2 years ago
GatherBy[{{2018, 9}, {2019, 9}, {2019, 10}, {2020, 9}}, First]
(* {{{2018, 9}}, {{2019, 9}, {2019, 10}}, {{2020, 9}}} *)

CountsBy[{{2018, 9}, {2019, 9}, {2019, 10}, {2020, 9}}, First]
(* <|2018 -> 1, 2019 -> 2, 2020 -> 1|> *)

GroupBy[{{2018, 9}, {2019, 9}, {2019, 10}, {2020, 9}}, First]
(* <|2018 -> {{2018, 9}}, 2019 -> {{2019, 9}, {2019, 10}}, 2020 -> {{2020, 9}}|> *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract