# Create missing columns (or rows) in a NamedArray with a default value

**URL:** <https://discourse.julialang.org/t/create-missing-columns-or-rows-in-a-namedarray-with-a-default-value/7128>\
**Category:** Data\
**Created:** [November 17, 2017, 2:53pm UTC](https://discourse.julialang.org/t/create-missing-columns-or-rows-in-a-namedarray-with-a-default-value/7128 "2017-11-17T14:53:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [November 17, 2017, 2:53pm UTC](https://discourse.julialang.org/t/create-missing-columns-or-rows-in-a-namedarray-with-a-default-value/7128/1 "2017-11-17T14:53:18Z")

</div>

Hello,

I would like to transform a Named Array (coming for FreqTable.jl freqtable) from

```julia
x = NamedArray([2 0; 0 1; 1 2]);
idx_rows = ["0", "1", "2"]
idx_cols = ["0", "2"]
setnames!(x, idx_rows, 1);
setnames!(x, idx_cols, 2);
x
3×2 Named Array{Int64,2}
Dim1 ╲ Dim2 │ 0 2
────────────┼─────
0 │ 2 0
1 │ 0 1
2 │ 1 2

```

to

```julia
3×2 Named Array{Int64,2}
Dim1 ╲ Dim2 │ 0 1 2
────────────┼────────
0 │ 2 0 0
1 │ 0 0 1
2 │ 1 0 2

```

With Python Pandas DataFrame I do this using

```python
df = df.loc[idx, idx.copy()].fillna(0)

```

But I haven’t found a correct way to do this with Julia.

I wonder if handling this is out of the scope of NamedArrays and if I should use a data structure handling NaN just for this ?

Kind regards

Pinging @davidavdav @nalimilan

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [November 18, 2017, 11:02am UTC](https://discourse.julialang.org/t/create-missing-columns-or-rows-in-a-namedarray-with-a-default-value/7128/2 "2017-11-18T11:02:42Z")

</div>

Hello,

This looks a bit outside the scope of `NamedArrays`, I suppose you can always construct a new array from the old one and insert a new column, and fix the names, but a one-line would start to look pretty ugly I suppose. The scope of NamedArrays is to be able to do what you can do with an Array, it is just that you can have names on the various rows/cols/… and the dimensions.

Would it help if there is an export to `DataFrames`, so that you can do such manipulation within that structure—that package should target to be more like Python pandas and R dataframes, I suppose.

—david

---

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [November 18, 2017, 2:51pm UTC](https://discourse.julialang.org/t/create-missing-columns-or-rows-in-a-namedarray-with-a-default-value/7128/3 "2017-11-18T14:51:40Z")

</div>

Hello,

Thanks for your answer.  
A workaround (instead of converting to DataFrame) could be to create a  
NamedArray filled with zeros and to add (in place) original NamedArray to  
this one (despite size are differents but given the fact that keys in  
original NamedArray also exists in new (and bigger) NamedArray.

About converting to DataFrame you might have a look at  
[https://github.com/davidanthoff/IterableTables.jl/issues/65](https://github.com/davidanthoff/IterableTables.jl/issues/65) and  
[https://github.com/davidavdav/NamedArrays.jl/issues/55](https://github.com/davidavdav/NamedArrays.jl/issues/55)

Kind regards
