# Chain the transformation without @chain macro

**URL:** <https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955>\
**Category:** General Usage\
**Tags:** dataframes, dataframesmeta, chaining\
**Created:** [March 12, 2023, 5:56am UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955 "2023-03-12T05:56:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 12, 2023, 5:56am UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955/1 "2023-03-12T05:56:38Z")

</div>

Reading the DataFramesMeta.jl document helps me learn a clean way to chain the transformation with the @chain macro.

From its document:

```julia
 @chain msleep begin
    @select :name :sleep_total
    @rsubset :sleep_total > 16
 end

```

I wonder whether I can achieve the same with `|>` operator so I try:

```julia
mammals_sleep[:, [:name, :sleep_total]] |> x -> filter(x -> x[!, :sleep_total] >= 10, x)

```

and get error

```julia
MethodError: no method matching getindex(::DataFrames.DataFrameRow{DataFrames.DataFrame, DataFrames.Index}, ::typeof(!), ::Symbol)

Closest candidates are:

getindex(::DataFrames.DataFrameRow, !Matched::Colon) at C:\Users\sofia\.julia\packages\DataFrames\LteEl\src\dataframerow\dataframerow.jl:225

getindex(::DataFrames.DataFrameRow, !Matched::Union{Colon, Regex, AbstractVector, DataAPI.All, DataAPI.Between, DataAPI.Cols, InvertedIndices.InvertedIndex}) at C:\Users\sofia\.julia\packages\DataFrames\LteEl\src\dataframerow\dataframerow.jl:215

getindex(::DataFrames.DataFrameRow, !Matched::Union{AbstractString, Signed, Symbol, Unsigned}) at C:\Users\sofia\.julia\packages\DataFrames\LteEl\src\dataframerow\dataframerow.jl:212

```

I know that `@chain` macro is a more convenient and intuitive way to do but I also wanted to experiment and learn a new syntax system from Julia, where Julia did great job. Thanks.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 12, 2023, 4:06pm UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955/2 "2023-03-12T16:06:51Z")

</div>

> [@hungpham3112](#):
>
> `x[!, :sleep_total]`

should be:

```julia
x.sleep_total

```

---

<div class="post-metadata">

**Author:** ![hungpham3112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hungpham3112/32/34315_2.png) [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Post date:** [March 12, 2023, 4:30pm UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955/3 "2023-03-12T16:30:56Z")

</div>

> [@hungpham3112](#):
>
> `mammals_sleep[:, [:name, :sleep_total]] |> x -> filter(x -> x[!, :sleep_total] >= 10, x)`

Thank you it works well. But please explain why it works. Is there any documentation for this behavior?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 12, 2023, 6:40pm UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955/4 "2023-03-12T18:40:56Z")

</div>

> [@hungpham3112](#):
>
> Is there any documentation for this behavior?

It is in documentation of `filter`:

> the predicate `fun` is passed `DataFrameRows`. Elements of a `DataFrameRow` may be accessed with dot syntax or column indexing inside `fun`.

```julia
  julia> df = DataFrame(x=[3, 1, 2, 1], y=["b", "c", "a", "b"])
  4×2 DataFrame
   Row │ x y
       │ Int64 String
  ─────┼───────────────
     1 │ 3 b
     2 │ 1 c
     3 │ 2 a
     4 │ 1 b

  julia> filter(row -> row.x > 1, df)
  2×2 DataFrame
   Row │ x y
       │ Int64 String
  ─────┼───────────────
     1 │ 3 b
     2 │ 2 a

  julia> filter(row -> row["x"] > 1, df)
  2×2 DataFrame
   Row │ x y
       │ Int64 String
  ─────┼───────────────
     1 │ 3 b
     2 │ 2 a

```

---

<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:** [March 12, 2023, 10:24pm UTC](https://discourse.julialang.org/t/chain-the-transformation-without-chain-macro/95955/5 "2023-03-12T22:24:40Z")

</div>

Perhaps with the use of the Pipe package you get something close to the desired chain transformation

```julia
using DataFrames
x = DataFrame(x=[3, 1, 1, 2], y=4:-1:1, z=1:4)
x[:,[:x]] |>x->filter(x->x.x>1,x)

using Pipe : @pipe

@pipe x |> select(_,[:x,:y]) |> filter(y->y.x>1,_)

```
