# DataFramesMeta selecting multiple columns

**URL:** <https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 18, 2018, 2:44am UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481 "2018-10-18T02:44:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 18, 2018, 2:44am UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/1 "2018-10-18T02:44:37Z")

</div>

Using DataFramesMeta (or others), can I select multiple columns of choice and do some calculations?

Here’s a mock data. I would like to subset by column x, and then calculate column-wise means. Here’s the answer.

`df = DataFrame(x = [1,1,1,2,3,2,3,2,3,], y1 = [2,1,2,1,2,1,2,1,2], y2 = [5,7,6,5,7,6,5,7,6], y3 = [4,2,3,4,2,3,4,2,3])`  
`l = [:y1, :y2, :y3]`  
`by(df, :x, d->mean(Array{Float64,2}(d[l]); dims=1))`

3×4 DataFrame  
│ Row │ x │ x1 │ x2 │ x3 │  
├──┼─┼────┼───┼──┤  
│ 1 │ 1 │ 1.66667 │ 6.0 │ 3.0 │  
│ 2 │ 2 │ 1.0 │ 6.0 │ 3.0 │  
│ 3 │ 3 │ 2.0 │ 6.0 │ 3.0 │

I also wanted to do this using @linq so that it can be coupled with other functions. But, mine didn’t work (obviously).

`@linq df |> by(:x, mean(Array{Float64,2}(l; dims=2)))`

I wanted to,

1. subset a data frame by column x
2. select columns of choice (y1, y2, y3)
3. calculate column-wise means for y1, y2, y3

Any idea?

Thanks,

---

<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:** [October 18, 2018, 2:54am UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/2 "2018-10-18T02:54:53Z")

</div>

Can you clarify what you mean by “select multiple columns of choice and do some calcuations”?

It’s unclear why you have `Array{Int,2}` in your function call, as well. What are you trying to achieve?

---

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 18, 2018, 3:14am UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/3 "2018-10-18T03:14:02Z")

</div>

@pdeffebach sorry about that. I edited my post. I think here’s a better representation.

I wanted to

1. subset a data frame by column x
2. select columns of choice (y1, y2, y3)
3. calculate column-wise means for y1, y2, y3

---

<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:** [October 18, 2018, 3:53am UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/4 "2018-10-18T03:53:18Z")

</div>

Ah I see. What you are asking for is difficult to do with DataFramesMeta. Better to use Base DataFrames instead:

```julia
cols = [:x2, :x3, :x4]
by(df, :a) do d
       DataFrame([mean(d[col]) for col in cols]', cols)
end

```

---

<div class="post-metadata">

**Author:** ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)\
**Post date:** [October 18, 2018, 1:44pm UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/5 "2018-10-18T13:44:07Z")

</div>

@pdeffebach Thanks!

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 18, 2018, 8:35pm UTC](https://discourse.julialang.org/t/dataframesmeta-selecting-multiple-columns/16481/6 "2018-10-18T20:35:42Z")

</div>

`colwise` and `aggregate` are also useful for this operation
