# DataFramesMeta question

**URL:** <https://discourse.julialang.org/t/dataframesmeta-question/72268>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [November 30, 2021, 4:09am UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268 "2021-11-30T04:09:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [November 30, 2021, 4:09am UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/1 "2021-11-30T04:09:37Z")

</div>

I am trying to learn DataFramesMeta LINQ similar syntax.

I have not been able to come up the proper counter of following very simple DataFrames syntax for DataFramesMeta

Get number of rows.

```julia
combine(df, nrow)

```

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [November 30, 2021, 5:28am UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/2 "2021-11-30T05:28:34Z")

</div>

You can drop the `combine`. Use `nrow(df)` to get the total number of rows in the the DataFrame. If you are after the number of rows belonging to each group, then you need to group the DataFrame.

```julia
using DataFrames
# Generate a DataFrame with lots of repeated values
df = DataFrame(:a => rand(1:10, 10000), :b => rand(1:10, 10000)) 
combine(groupby(df, :a), nrow)

```

---

<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:** [November 30, 2021, 10:26am UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/3 "2021-11-30T10:26:30Z")

</div>

Probably @pdeffebach is best to be asked here. I think he has not implemented a special syntax for this yet, so the simplest is `@combine(df, :nrow = length(:some_column_in_the_data_frame))` (but I might be wrong and there is some special case for this).

---

<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:** [November 30, 2021, 3:09pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/4 "2021-11-30T15:09:44Z")

</div>

There is no special case for this. There is no equivalent to Stata’s `_N` or dplyr’s `n()`.

You can do

```julia
@combine df $nrow

```

But that kind of thing can’t be used inside transformations, so you can’t do

```julia
@combine df :y = :x / $nrow

```

I would only use

```julia
@combine df $nrow

```

if you really know what you are doing and exactly how DataFramesMeta.jl translates expressions to actually DataFrames.jl calls.

So stick to `length` for now.

---

<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:** [November 30, 2021, 3:33pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/5 "2021-11-30T15:33:51Z")

</div>

I think `@combine df $nrow` is fine. Is this style `@combine df $fun` translated to `combine(df, fun)` in DataFrames.jl?

---

<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:** [November 30, 2021, 3:39pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/6 "2021-11-30T15:39:12Z")

</div>

Yes. It gets passed straight through.

---

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [November 30, 2021, 3:39pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/7 "2021-11-30T15:39:42Z")

</div>

Thank you for the response. nrow may be needed as a column in some output. I know that nrow can be extracted various ways. I am trying to translate the examples in DataFrames documentation to DataFramesMeta so nrow as a colum often comes up.

---

<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:** [November 30, 2021, 3:48pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/8 "2021-11-30T15:48:52Z")

</div>

@pdeffebach - for my reference can you please comment why `@combine(df, nrow)` does not work?

---

<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:** [November 30, 2021, 3:54pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/9 "2021-11-30T15:54:42Z")

</div>

`@combine(df, nrow)` does not work because I don’t want to allow arbitrary expressions that don’t get passed through the parsing framework. Think about `@subset` and `@orderby`.

```julia
@subset df my_fun(:x)

```

gets transformed into

```julia
subset(df, :x => (t -> myfun(t)))

```

And then consequently

```julia
@subset df myfun

```

should be passed

```julia
subset(df, [] => (t -> myfun))

```

You would be asking for something like

```julia
@subset df myfun

```

to get transformed into

```julia
subset(df, myfun)

```

which is at odds with the rest of the parsing.

Basically, there is one rule for parsing without `$` and it would add a lot of complexity to make exceptions.

---

<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:** [November 30, 2021, 5:25pm UTC](https://discourse.julialang.org/t/dataframesmeta-question/72268/10 "2021-11-30T17:25:36Z")

</div>

OK - great!
