# Concatenating vectors of different length into a dataframe or table

**URL:** <https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 5, 2020, 2:54am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760 "2020-10-05T02:54:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![horizon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horizon/32/15971_2.png) [@horizon](https://discourse.julialang.org/u/horizon)\
**Post date:** [October 5, 2020, 2:54am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/1 "2020-10-05T02:54:15Z")

</div>

Hi,  
I am trying to concatenate vectors of different length (can we merge vectors of different lengths?) into a dataframe or table but nothing seems to work. I will share my code.

```julia

g = loadtable("all_files" ; spacedelim = true, filenamecol = "files")
df1 = DataFrame(g)

Age = DataFrame()
r = []
for i = 2:length(g)
        global Age , r
        if [g.columns.files[i-1]] == [g.columns.files[i]]
                r = append!(r , g.columns.logAge[i-1])
        else
                Age[:,i-1] = r
                r = []
        end
end

```

Thank you.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 5, 2020, 3:23am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/2 "2020-10-05T03:23:44Z")

</div>

Could you edit your post to put the code in a code block? That makes the code easier to read. It should look something like this:

```julia
using DataFrames

df = DataFrame(a=1:3, b = 4:5)

```

You can create a code block by placing triple backticks before and after the code, or by using the “Preformatted text” button in the GUI.

Also, it helps if the code you provide is runnable. Unfortunately, we can’t run this line

```julia
g = loadtable(“all_files” ; spacedelim = true, filenamecol = “files”)

```

so that makes it more difficult to help.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 5, 2020, 3:26am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/3 "2020-10-05T03:26:42Z")

</div>

Here’s some more guidance on questions that will help us to help you. 🙂

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![horizon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horizon/32/15971_2.png) [@horizon](https://discourse.julialang.org/u/horizon)\
**Post date:** [October 5, 2020, 8:56pm UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/4 "2020-10-05T20:56:16Z")

</div>

I put the code in code block.  
Thank you for your help.

---

<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:** [October 5, 2020, 8:59pm UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/5 "2020-10-05T20:59:46Z")

</div>

I can’t run your code and I don’t fully understand it because you haven’t posted an MWE that I can easily copy and paste and run everything.

A data frame can’t store columns of different lengths. This might be your problem.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 5, 2020, 9:06pm UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/6 "2020-10-05T21:06:24Z")

</div>

Here’s an example of what happens if you try to extend a column of a data frame without extending the other columns by the same amount:

```julia
julia> using DataFrames

julia> df = DataFrame(a=1:2, b=3:4)
2×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 3 │
│ 2 │ 2 │ 4 │

julia> append!(df.b, [5, 6])
4-element Array{Int64,1}:
 3
 4
 5
 6

julia> df
Error showing value of type DataFrame:
ERROR: AssertionError: Data frame is corrupt: length of column
:b (4) does not match length of column 1 (2). The column vector
has likely been resized unintentionally (either directly or
because it is shared with another data frame).

```

Note that the data frame gets corrupted after you extend column `b`.

---

<div class="post-metadata">

**Author:** ![horizon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horizon/32/15971_2.png) [@horizon](https://discourse.julialang.org/u/horizon)\
**Post date:** [October 5, 2020, 9:12pm UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/7 "2020-10-05T21:12:29Z")

</div>

what is MWE.  
yeah I thought so. Is there any other way to store columns of different length into an array.

Thank you.

---

<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:** [October 5, 2020, 9:14pm UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/8 "2020-10-05T21:14:32Z")

</div>

An MWE ns a “minimum working example”. and just means a small set of code that I can copy and paste (without having to download any other files etc.) that results in the error you are experiencing.

You can use a Dictionary

```julia

julia> Dict("a" => [1, 2], "b" => [1, 2, 3])
Dict{String,Array{Int64,1}} with 2 entries:
  "b" => [1, 2, 3]
  "a" => [1, 2]

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 6, 2020, 12:21am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/9 "2020-10-06T00:21:25Z")

</div>

Doesn’t `vcat` pad DataFrames with `missing`? Could that be helpful here?

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [October 6, 2020, 1:32am UTC](https://discourse.julialang.org/t/concatenating-vectors-of-different-length-into-a-dataframe-or-table/47760/10 "2020-10-06T01:32:40Z")

</div>

Other tricks with DataFrames:

```julia
series = [[4,5], [7,8,9]]
rows = [[1:length(s);] for s in series]
df = flatten(DataFrame(g=["s1","s2"], s=series, r=rows), [:s, :r])
 unstack(df, :g, :s)

```

For more info: `?flatten` and `?unstack`
