# Pre-processing data within a Julia structure

**URL:** <https://discourse.julialang.org/t/pre-processing-data-within-a-julia-structure/109979>\
**Category:** New to Julia\
**Created:** [February 9, 2024, 12:30pm UTC](https://discourse.julialang.org/t/pre-processing-data-within-a-julia-structure/109979 "2024-02-09T12:30:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RogerV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogerv/32/206617_2.png) [@RogerV](https://discourse.julialang.org/u/RogerV)\
**Post date:** [February 9, 2024, 12:30pm UTC](https://discourse.julialang.org/t/pre-processing-data-within-a-julia-structure/109979/1 "2024-02-09T12:30:18Z")

</div>

I would like to have a Julia structure with data and parameters contained with it, to be used with various functions. E.g., I start with data frame of numbers, but many functions use it as a matrix and also need to know its dimensions. In python I would use something like:

```julia
class my_class(object):
    def __init__ (self, df):
        X = df.to_numpy()
        self.n, self.m = X.shape
        row_names = df.index.tolist()
        col_names = df.columns.tolist()

Foo = my_class(df)

```

What poses a difficulty is whether operations `.to_numpy()`, `.shape`, etc. can be performed in the Julia structure or whether beforehand. Or perhaps this necessitates using a mutable structure?

At my current level of knowledge I would do something like this:

```julia
struct MyClass
    df::DataFrame
    X::Matrix{Float64}
    n::Int64
    m::Int64
    row_names::Vector{String}
    col_names::Vector{String}
end

X = Matrix{Float64}(df[:,begin+1:end])
n, m = size(X)
row_names = df[:,begin]
col_names = names(SAT[:,begin+1:end])

Foo = MyClass(df, X, n, m, row_names, col_names)

```

Remark: thinking of it, I could probably wrap the code above in a function, which returns the initialized structure Foo. But it seems like a convoluted way of doing it.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 9, 2024, 12:37pm UTC](https://discourse.julialang.org/t/pre-processing-data-within-a-julia-structure/109979/2 "2024-02-09T12:37:58Z")

</div>

> [@RogerV](#):
>
> Remark: thinking of it, I could probably wrap the code above in a function, which returns the initialized structure Foo. But it seems like a convoluted way of doing it.

The function you are talking about is called a [_constructor_](https://docs.julialang.org/en/v1/manual/constructors/) for the type `MyClass` and this is indeed the way to approach it. Note that you have the function ` __init__ ` in python that serves the same purpose?

---

<div class="post-metadata">

**Author:** ![RogerV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogerv/32/206617_2.png) [@RogerV](https://discourse.julialang.org/u/RogerV)\
**Post date:** [February 9, 2024, 1:24pm UTC](https://discourse.julialang.org/t/pre-processing-data-within-a-julia-structure/109979/3 "2024-02-09T13:24:24Z")

</div>

Thank you! Based on the explanations in the link that you provided, the answer coudl be like this:

```julia
struct MyClass
    df::DataFrame
    X::Matrix{Float64}
    n::Int64
    m::Int64
    row_names::Vector{String}
    col_names::Vector{String}
    function MyClass(df::DataFrame)
        X = Matrix{Float64}(df[:,begin+1:end])
        n, m = size(X)
        row_names = df[:,begin]
        col_names = names(SAT[:,begin+1:end])
        new(df, X, n, m, row_names, col_names)
    end
end

Foo = MyClass(df)

```
