Forum: Ruby Accumulate total count for each month

Posted by Soichi Ishida (soichi)
on 2012-12-26 04:16
I have a file that contains a list of STRING datetime like

Thu Dec 20 00:58:17 +0000 2012
Thu Dec 20 00:50:18 +0000 2012
Thu Dec 20 00:48:53 +0000 2012
Thu Dec 20 00:44:43 +0000 2012
Thu Dec 20 00:42:32 +0000 2012
...

Each line represents the occurrence of one event.
I want to count the number of events occurred each month (or day).

First, I have converted to DateTime objects. It's easy.

Normally, when counting the number of each word occurred in a file,
using hash can help (each word as a key).  But this case, it is not that
simple because the key is date object.

Do you have better ideas for counting the number of events each month?

soichi
Posted by 7stud -- (7stud)
on 2012-12-26 08:34
Soichi Ishida wrote in post #1090212:
>
> Normally, when counting the number of each word occurred in a file,
> using hash can help (each word as a key).  But this case, it is not that
> simple because the key is date object.
>
> Do you have better ideas for counting the number of events each month?
>

require 'date'

date_times1 = (1..12).map do |month|
  DateTime.new(2012,month,25,4,5,6)
end

date_times2 = (1..6).map do |month|
  DateTime.new(2012,month,15,4,5,6)
end

date_times = date_times1 + date_times2

results = Hash.new(0)

date_times.each do |dt|
  results[dt.month] += 1
end

(1..12).each do |month|
  puts "month %s: %s" % [month, results[month]]
end

--output:--
month 1: 2
month 2: 2
month 3: 2
month 4: 2
month 5: 2
month 6: 2
month 7: 1
month 8: 1
month 9: 1
month 10: 1
month 11: 1
month 12: 1
Posted by Carlo E. Prelz (Guest)
on 2012-12-26 08:50
(Received via mailing list)
Subject: Accumulate total count for each month
  Date: Wed 26 Dec 12 12:16:26PM +0900

Quoting Soichi Ishida (lists@ruby-forum.com):

> Normally, when counting the number of each word occurred in a file,
> using hash can help (each word as a key).  But this case, it is not that
> simple because the key is date object.
>
> Do you have better ideas for counting the number of events each
> month?

Why not use the year+month as the hash key? From your DateTime object,
you obtain an appropriate string with dt.strftime('%Y%m').

Carlo
Posted by 7stud -- (7stud)
on 2012-12-26 08:54
...

(1..12).each do |month|
  puts "%s: %s" % [Date::MONTHNAMES[month], results[month]]
end

--output:--
January: 2
February: 2
March: 2
April: 2
May: 2
June: 2
July: 1
August: 1
September: 1
October: 1
November: 1
December: 1
Posted by Xavier Noria (fxn)
on 2012-12-26 10:59
(Received via mailing list)
If that is all there is to it, a simple split would work

nevents = Hash.new(0)

ARGF.each_line do |line|
  month, year = line.chomp.split.values_at(1, -1)
  nevents["#{year} #{month}"] += 1
end
Posted by Soichi Ishida (soichi)
on 2012-12-26 11:02
Wow, thanks everyone.
There are a lot of ideas I could not figure out myself!

soichi
Posted by Robert Klemme (robert_k78)
on 2012-12-26 13:15
(Received via mailing list)
On Wed, Dec 26, 2012 at 4:16 AM, Soichi Ishida <lists@ruby-forum.com> 
wrote:
> I want to count the number of events occurred each month (or day).
>
> First, I have converted to DateTime objects. It's easy.

Better use Date since you want to count occurrences at dates.

> Normally, when counting the number of each word occurred in a file,
> using hash can help (each word as a key).  But this case, it is not that
> simple because the key is date object.

Why isn't that simple?  Did you actually try it?

> Do you have better ideas for counting the number of events each month?

require 'date'

counts = Hash.new 0

ARGF.each do |line|
  line.chomp!
  counts[Date.parse(line)] += 1
end

counts.sort_by {|d,| k}.each do |d, c|
  printf "%-20s %6d\n", d, c
end

Cheers

robert
Posted by 7stud -- (7stud)
on 2012-12-26 22:58
Robert Klemme wrote in post #1090246:
>
>
> Why isn't that simple?  Did you actually try it?
>
> require 'date'
>
> counts = Hash.new 0
>
> ARGF.each do |line|
>   line.chomp!
>   counts[Date.parse(line)] += 1
> end
>
> counts.sort_by {|d,| k}.each do |d, c|
>   printf "%-20s %6d\n", d, c
> end
>
> Cheers
>
> robert
>

1.rb:10:in `block in <main>': undefined local variable or method `k' for
main:Object (NameError)

.
.
.
.
.
.
Posted by Robert Klemme (robert_k78)
on 2012-12-27 12:02
(Received via mailing list)
On Wed, Dec 26, 2012 at 10:59 PM, 7stud -- <lists@ruby-forum.com> wrote:
>>   line.chomp!
>>
>
> 1.rb:10:in `block in <main>': undefined local variable or method `k' for
> main:Object (NameError)

Typo when ad hoc refactoring names

require 'date'

counts = Hash.new 0

ARGF.each do |line|
  line.chomp!
  counts[Date.parse(line)] += 1
end

counts.sort_by {|d,| d}.each do |d, c|
  printf "%-20s %6d\n", d, c
end

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.