# How to \`combine\` row vectors

**URL:** <https://discourse.julialang.org/t/how-to-combine-row-vectors/123940>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [December 18, 2024, 12:45am UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940 "2024-12-18T00:45:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [December 18, 2024, 12:45am UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/1 "2024-12-18T00:45:17Z")

</div>

Hi All,

Just wondering how I can apply a function to rows containing vectors when doing a groupby → combine:

```julia
using DataFrames
df = DataFrame(group = ["A", "A", "B", "B"], v1 = [[1, 2], [3, 4], [5, 6], [7, 8] ], v2 = [[1, 2], [3, 4], [5, 6], [7, 8] ])
gdf = groupby(df, :group)
DataFrames.combine(gdf, 2:3 .=> sum)

```

returns

```julia
4×3 DataFrame
 Row │ group v1_sum v2_sum 
     │ String Int64 Int64  
─────┼────────────────────────
   1 │ A 4 4
   2 │ A 6 6
   3 │ B 12 12
   4 │ B 14 14

```

but I would like two rows, with column 2 and 3 containing vectors of length 2

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 18, 2024, 7:59am UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/2 "2024-12-18T07:59:17Z")

</div>

The following works:

```julia
julia> DataFrames.combine(gdf, 2:3 .=> Ref∘sum)
2×3 DataFrame
 Row │ group v1_Ref_sum v2_Ref_sum 
     │ String Array… Array…     
─────┼────────────────────────────────
   1 │ A [4, 6] [4, 6]
   2 │ B [12, 14] [12, 14]

```

But there does seem to be some conceptual problem.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 18, 2024, 3:14pm UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/3 "2024-12-18T15:14:55Z")

</div>

If it’s easier for you to follow, the following form also produces what you ask for.

```julia
combine(g->(;v1=[sum(g.v1)], v2=[sum(g.v2)]),gdf)

```

PS  
I, like Dan, have some difficulty imagining the logic behind the request

PPS  
I don’t know if in this case some variant of the unstack function (that accepts multiple columns) can get the result in one pass.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 18, 2024, 4:49pm UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/4 "2024-12-18T16:49:07Z")

</div>

another way: split-(apply)combine-

```julia
dfspl=select(df, :group,[:v1,:v2] .=>ByRow(x->(x...,)).=>x->x.*string.([1,2]) )

cdfspl=combine(groupby(dfspl,:group), Cols(2:5).=>sum .=>identity)

jcdf=select(cdfspl,:group, [Cols(2:3),Cols(4:5)].=>ByRow((x...)->Base.vect(x)).=>x->x[1][1:end-1])

```

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [December 18, 2024, 4:58pm UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/5 "2024-12-18T16:58:06Z")

</div>

> [@Dan](#):
>
> seem to be some conceptual problem.

The data frame that I’m working with has columns that store time series vectors. My goal is simply to groupby and sum retaining the original dimensionality of the time series. Is that sensible?

I would have never thought of  
`Ref ∘ sum`

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 18, 2024, 9:24pm UTC](https://discourse.julialang.org/t/how-to-combine-row-vectors/123940/6 "2024-12-18T21:24:58Z")

</div>

```julia
julia> df = DataFrame(group = ["A", "A", "B", "B"], v1 = [[1, 2], [3, 4], [5, 6], [7, 8] ], v2 = [[11, 21], [31, 41], [51, 61], [71, 81] ])   
4×3 DataFrame
 Row │ group v1 v2       
     │ String Array… Array…
─────┼──────────────────────────
   1 │ A [1, 2] [11, 21]
   2 │ A [3, 4] [31, 41]
   3 │ B [5, 6] [51, 61]
   4 │ B [7, 8] [71, 81]

julia> sdf=stack(df,[:v1,:v2])
8×3 DataFrame
 Row │ group variable value    
     │ String String Array…
─────┼────────────────────────────
   1 │ A v1 [1, 2]
   2 │ A v1 [3, 4]
   3 │ B v1 [5, 6]
   4 │ B v1 [7, 8]
   5 │ A v2 [11, 21]
   6 │ A v2 [31, 41]
   7 │ B v2 [51, 61]
   8 │ B v2 [71, 81]

julia> unstack(sdf,:group,:variable,:value, combine=sum)
2×3 DataFrame
 Row │ group v1 v2
     │ String Array…? Array…?
─────┼──────────────────────────────
   1 │ A [4, 6] [42, 62]
   2 │ B [12, 14] [122, 142]

```

the inverse operation

```julia
flatten(unstack(sdf,:group,:variable,:value, combine=copy),[:v1,:v2])

```
