# Creating a DataFrame column as the running mean of another column

**URL:** https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058
**Category:** Data
**Tags:** dataframes
**Created:** [January 23, 2022, 8:33am UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058 "2022-01-23T08:33:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [January 23, 2022, 8:33am UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/1 "2022-01-23T08:33:11Z")

</div>

The original code for this is a Pluto notebook, and the code in this post is supposed to be three notebook cells. The code works, and I’m pretty sure there’s a nicer syntax for the cell three, but I just can’t seem to figure out what it is.

```julia
using DataFrames, CSV, Chain, Dates, RollingFunctions

```

```julia
longdf = DataFrame(CSV.File(download("https://raw.githubusercontent.com/StatisticalMice/finland-covid-19/main/fact_epirapo_covid19case.csv")))

```

```julia
begin
	df = @chain longdf begin
		unstack(:Mittari, :val)
		select(:Aika => :Date, "Tapausten lukumäärä" => :Cases, "Testausmäärä" => :Tests, "Kuolemantapausten lukumäärä" => :Deaths)
		subset(:Date => d -> d.>= Date(2021, 8, 1))
		disallowmissing()
	end
	df.CasesRunMean = runmean(df.Cases, 5)
	df
end

```

 ![Screen Shot 2022-01-23 at 10.30.06](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2e3bc0631dff40d6a7eb606e88c8fb505b5b4cb.png)

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [January 23, 2022, 7:17pm UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/2 "2022-01-23T19:17:30Z")

</div>

> [@StatisticalMouse](#):
>
> `df.CasesRunMean = runmean(df.Cases, 5)`

With vanilla DataFrames.jl it would be in your chain:

```julia
transform!(:Cases => (x -> runmean(x, 5)) => :CasesRunMean)

```

or

```julia
@aside _.CasesRunMean = runmean(_.Cases, 5)

```

With e.g. DataFramesMeta.jl it would be:

```julia
@transform!(:CasesRunMean = runmean(:Cases, 5))

```

(not tested as your code is not reproducible, so please comment if it does not work 😄)

* * *

and

```julia
subset(:Date => d -> d.>= Date(2021, 8, 1))

```

can be written as

```julia
subset(:Date => ByRow(>=(Date(2021, 8, 1)))

```

or in DataFramesMeta.jl

```julia
@rsubset(:Date >= (Date(2021, 8, 1))

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 23, 2022, 7:23pm UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/3 "2022-01-23T19:23:51Z")

</div>

@StatisticalMouse, the code already looks nice (_except possibly for the Finish words we do not understand…_).

As a side note, there doesn’t seem to be a big payoff here in using @chain? The following plain code produces the same result:

```julia
dg = unstack(longdf,:Mittari, :val)
select!(dg, :Aika => :Date, "Tapausten lukumäärä" => :Cases, "Testausmäärä" => :Tests, "Kuolemantapausten lukumäärä" => :Deaths)
subset!(dg, :Date => d -> d.>= Date(2021, 8, 1))
dg.CasesRunMean = runmean(dg.Cases, 5)
dg == df # true

```

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [January 23, 2022, 7:46pm UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/4 "2022-01-23T19:46:22Z")

</div>

> [@rafael.guerra](#):
>
> The following plain code produces the same result:

As a side note: it is more efficient as it uses `select!` and `subset!` which do less copying 😄. We can safely do this as `unstack` allocates a new data frame so there is no risk of mutating the source.

---

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [January 23, 2022, 7:50pm UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/5 "2022-01-23T19:50:51Z")

</div>

Hmm, thanks, where to start replying… From the least important maybe. 😄

The Finnish words are from the gov agency CSV. ‘Määrä’ and ‘lukumäärä’ both mean ‘number of’, and otherwise it should be a direct translation. You’re welcome. 😃

When I started writing this yesterday evening it was like any other time I’ve wanted to do something with DataFrames, I was unable to decide which combination of packages to use.

I like using `@chain` because it uses `begin` and `end`, and then I don’t need to add them because Pluto requires them. It just feels cleaner. Except of course in this case I needed the extra pair.

I did make an attempt to make it reproducible, but of course I’ve forgotten to post the link to the repo itself. [https://github.com/StatisticalMice/finland-covid-19](https://github.com/StatisticalMice/finland-covid-19)

I did some versions with DataFramesMeta.jl and DataFrameMacros.jl. Both seem to have benefits. In the end I wanted just the base package as I can’t choose. I’ll fix the code tomorrow, it’s late.

---

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [January 24, 2022, 7:35pm UTC](https://discourse.julialang.org/t/creating-a-dataframe-column-as-the-running-mean-of-another-column/75058/6 "2022-01-24T19:35:42Z")

</div>

I settled for this version as it seems cleanest to me.

```julia
df = @chain longdf begin
	unstack(:Mittari, :val)
	select(:Aika => :Date, "Tapausten lukumäärä" => :Cases, "Testausmäärä" => :Tests, "Kuolemantapausten lukumäärä" => :Deaths)
	@aside _.CasesRunMean = runmean(_.Cases, 5)
	@aside _.TestsRunMean = runmean(_.Tests, 5)
	subset(:Date => ByRow(>=(Date(2021, 8, 1))))
	disallowmissing()
end

```
