# How to carry out a sum over data indexed by time and grouped by year and month?

**URL:** <https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 26, 2022, 3:27pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071 "2022-04-26T15:27:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [April 26, 2022, 3:27pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/1 "2022-04-26T15:27:53Z")

</div>

Hello, everyone

I am new to Julia and I’m trying to carry out the task of grouping a variable by year and month and then summing for each month and year along the duration of a time-index variable.

I know how to do this in R and I provide a small reproducible example bellow. In a nutshell, the variable `x` is indexed by date. With that, I can get monthly sums of `x` for the years 2021 and 2022 by grouping my data by `year` and `month`. Helped by `dplyr` and `lubridate`, I can then easily sum all `x` in that period.

**How would I do that in Julia?** While I don’t expect someone to fully reproduce this example, I’d appreciate some directions as to what packages I should look into? From what I’ve gathered, it’s basically either `Queryverse` and/or `DataFramesMeta`.

```julia
# Libraries
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#> filter, lag
#> The following objects are masked from 'package:base':
#> 
#> intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#> date, intersect, setdiff, union

# Generate fake data
x = runif(2*365,0, 50)

# Generate a sequence of dates
my_seq = seq(as.Date("2021/1/1"), by = "day", length.out = length(x))

# Set a dataframe
my_data = tibble(my_seq, x)

# Use tidyr to group data points by month and sum them for each 
# month.
monthly_x =
  my_data |>
  mutate(year = year(my_seq), 
         month = month(my_seq)) |>
  group_by(year, month) |>
  summarise(sum_of_x = sum(x))
#> `summarise()` has grouped output by 'year'. You can override using the
#> `.groups` argument.

# Check January 2021
sum(x[1:31]) == monthly_x$sum_of_x[1]
#> [1] TRUE
#
#Created on 2022-04-26 by the reprex package (v2.0.1)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 26, 2022, 4:09pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/2 "2022-04-26T16:09:18Z")

</div>

It’s much the same:

```julia
my_data[:, :year] = year.(my_data.my_seq)
my_data[:, month] = month.(my_data.my_seq)
combine(groupby(my_data, [:year, :month]), :x => sum => sum_of_x)

```

There are various packages that allow for a more “tidyr” style chaining of commands, like e.g. Chain.jl (which also has a useful comparison between the different chaining packages in it’s Readme, so check out the [GitHub page](https://github.com/jkrumbiegel/Chain.jl))

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 26, 2022, 6:30pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/3 "2022-04-26T18:30:10Z")

</div>

Here is an example with `DataFramesMeta.jl` and `Chain.jl`

```julia
@chain df begin 
    @rtransform :year = year(:my_seq)
    @rtransform :month = month(:my_seq)
    groupby([:year, :month])
    @combine :sum_of_x = sum(:x)
end

```

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [April 26, 2022, 6:42pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/4 "2022-04-26T18:42:55Z")

</div>

Thanks, @nilshg

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [April 26, 2022, 6:43pm UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/5 "2022-04-26T18:43:50Z")

</div>

Thanks, @pdeffebach

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [April 27, 2022, 5:47am UTC](https://discourse.julialang.org/t/how-to-carry-out-a-sum-over-data-indexed-by-time-and-grouped-by-year-and-month/80071/6 "2022-04-27T05:47:24Z")

</div>

Alternatively, with arrays/dictionaries instead of dataframes:

```julia
julia> using Dates, DataPipes, SplitApplyCombine, StructArrays

# create table:
julia> data = [
    (; my_seq, x=rand())
    for my_seq in range(Date(2021, 1, 1), step=Day(1), length=50)
] |> StructArray

# compute sums:
julia> @p data |>
           groupview((year=year(_.my_seq), month=month(_.my_seq))) |>
           map(sum(_.x))

```
