# Sum statistic over a day

**URL:** <https://discourse.julialang.org/t/sum-statistic-over-a-day/19409>\
**Category:** General Usage\
**Tags:** dates, data, dataframes\
**Created:** [January 9, 2019, 3:00am UTC](https://discourse.julialang.org/t/sum-statistic-over-a-day/19409 "2019-01-09T03:00:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![natemcintosh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natemcintosh/32/9021_2.png) [@natemcintosh](https://discourse.julialang.org/u/natemcintosh)\
**Post date:** [January 9, 2019, 3:00am UTC](https://discourse.julialang.org/t/sum-statistic-over-a-day/19409/1 "2019-01-09T03:00:58Z")

</div>

I have a DataFrame as such

```julia
│ Row │ start │ finish │ cals │ DOW │
│ │ DateTime │ DateTime │ Float64 │ Int64 │
├─────┼─────────────────────┼─────────────────────┼─────────┼───────┤
│ 1 │ 2017-07-19T16:58:00 │ 2017-07-19T17:58:00 │ 0.073 │ 3 │
│ 2 │ 2017-07-19T18:08:00 │ 2017-07-19T18:09:00 │ 0.163 │ 3 │
│ 3 │ 2017-07-19T18:09:00 │ 2017-07-19T18:18:00 │ 0.057 │ 3 │
│ 4 │ 2017-07-19T18:18:00 │ 2017-07-19T18:19:00 │ 0.443 │ 3 │
│ 5 │ 2017-07-19T18:19:00 │ 2017-07-19T18:20:00 │ 1.086 │ 3 │

```

The `:cals` column represents calories burned, and the `:DOW` represents the day of the week.  
What I would like to do is sum up the calories for each day, and then group by day of the week.

Hence, after summing, a new DataFrame might look like

```julia
│ Row │ day │ cals │ DOW │
│ │ DateTime │ Float64 │ Int64 │
├─────┼────────────┼─────────┼───────┼
│ 1 │ 2017-07-19 │ 532 │ 3 │
│ 2 │ 2017-07-20 │ 234 │ 4 │
│ 3 │ 2017-07-21 │ 765 │ 5 │
│ 4 │ 2017-07-22 │ 123 │ 6 │
│ 5 │ 2017-07-23 │ 567 │ 7 │

```

However, I’m getting stuck on how to sum up the calories over each individual day.  
Does anyone have any suggestions?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [January 9, 2019, 3:19am UTC](https://discourse.julialang.org/t/sum-statistic-over-a-day/19409/2 "2019-01-09T03:19:37Z")

</div>

```julia
using Dates, DataFrames

data = DataFrame(start = DateTime(2000,1,1,0):Hour(1):DateTime(2000,1,14,23))
data.finish = data.start + Hour(1)
data.cals = 1:size(data, 1)
data.day = Date.(data.start)
output = by(data, :day, cals = :cals => sum)
output.dow = categorical(dayname.(dayofweek.(output.day)))
output = by(output, :dow, cals = :cals => sum)

```

You can adjust the code to sum directly by day of week or just the calendar day.

---

<div class="post-metadata">

**Author:** ![natemcintosh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natemcintosh/32/9021_2.png) [@natemcintosh](https://discourse.julialang.org/u/natemcintosh)\
**Post date:** [January 10, 2019, 12:06am UTC](https://discourse.julialang.org/t/sum-statistic-over-a-day/19409/3 "2019-01-10T00:06:18Z")

</div>

Ohhhh, this makes a lot of sense.

Thanks for the help!!
