# Structures and DataFrames

**URL:** https://discourse.julialang.org/t/structures-and-dataframes/82166
**Category:** General Usage
**Tags:** dataframes, structure
**Created:** [June 2, 2022, 11:22pm UTC](https://discourse.julialang.org/t/structures-and-dataframes/82166 "2022-06-02T23:22:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [June 2, 2022, 11:22pm UTC](https://discourse.julialang.org/t/structures-and-dataframes/82166/1 "2022-06-02T23:22:45Z")

</div>

Learning about structures. Appreciate any comments on the below

```julia
struct BASE
    a::Int64
    b::Int64
end

struct B1
    base::BASE
    b1::Int64
end

struct B2
    base::BASE
    b2::Int64
end

df = DataFrame( type = [:B1,:B2], a=[1,1], b=[1,1], b1=[1,missing], b2=[missing,2] )

value( x::B1 ) = x.base.a + x.b1 * 2
value( x::B2 ) = x.base.a + x.b2 * 3

```

Question 1  
is there an easy way to put each row of df into the appropriate structure (B1, B2) ?  
I’d like to have a new column of df with the structures.  
then apply the value function to this column.

Question 2  
is it possible to avoid the x in the value function. Something like  
`value( ::B1... ) = base.a + b1 * 2`

Question 3  
I believe putting base::BASE into B1 and B2 like this is the best way to do inheritance in Julia (please correct me otherwise).

Question 4  
Is it possible to include BASE in other structures but not require a prefix `base.` to access the `a` and `b` parameters. Assuming the B1 parameter set is unique e.g. `a, b, b1`

My actual example is FX Options. I’d like to have a struct for the parameters common to all (Strike, Expiry, Settlement etc) and paste this into structs for the various types (Barrier, Digital, Asian etc).  
I’d prefer not to preface the base parameters with `base.`

---

<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: [June 3, 2022, 12:33am UTC](https://discourse.julialang.org/t/structures-and-dataframes/82166/2 "2022-06-03T00:33:20Z")

</div>

> [@Lincoln\_Hannah](#):
>
> Question 1  
> is there an easy way to put each row of df into the appropriate structure (B1, B2) ?  
> I’d like to have a new column of df with the structures.  
> then apply the value function to this column.

```julia
julia> @rtransform df :s = begin
           base = BASE(:a, :b)
           :type == ^(:B1) ? B1(base, :b1) : B2(base, :b2)
       end
2×6 DataFrame
 Row │ type a b b1 b2 s                 
     │ Symbol Int64 Int64 Int64? Int64? Any               
─────┼───────────────────────────────────────────────────────────
   1 │ B1 1 1 1 missing B1(BASE(1, 1), 1)
   2 │ B2 1 1 missing 2 B2(BASE(1, 1), 2)

```

> [@Lincoln\_Hannah](#):
>
> is it possible to avoid the x in the value function. Something like  
> `value( ::B1... ) = base.a + b1 * 2`

No.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [June 3, 2022, 7:29am UTC](https://discourse.julialang.org/t/structures-and-dataframes/82166/3 "2022-06-03T07:29:39Z")

</div>

thanks Peter. helpful as always:)
