# DataFrame creating a rowmean for 95 columns

**URL:** <https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [March 7, 2021, 12:55pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671 "2021-03-07T12:55:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![korilium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korilium/32/48714_2.png) [@korilium](https://discourse.julialang.org/u/korilium)\
**Post date:** [March 7, 2021, 12:55pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/1 "2021-03-07T12:55:23Z")

</div>

Hello,

I am quite new to Julia and I was wondering how to implement a rowmean using a dataframe type. For Arrays, this is quite easy but I can not seem to figure out how to do it with a dataframe because most transformations work with the columns instead of the rows. I tried to transpose the data using the permutedims function, but I got an error

```julia

test = SP500Return[week2:end,groupedbeta[1].name]

permutedims(test, 1)

ArgumentError: src_namescol must have eltype `Symbol` or `<:AbstractString`

```

I have 95 columns which I have grouped in 10 portfolio’s based on the beta’s of the stocks (each column is a stock in my portfolio), so renaming them becomes quite annoying. Does somebody has an idea on how to solve this issue.

I also tried:

```julia
combine(SP500Return[week2:end,groupedbeta[1].name], groupedbeta[1].name .=> ByRow(mean)) 

```

But then he returns the DataFrame back.

Thank you in advance

---

<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:** [March 7, 2021, 4:06pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/2 "2021-03-07T16:06:56Z")

</div>

Sorry I’m a bit confused as for what you are asking.

Do you mean like Stata’s `rowmean`

```julia
egen x = rowmean(`vars')

```

There is no transpose for DataFrames.

I _think_ you want `AsTable`

```julia
julia> df = DataFrame(rand(1000, 100), :auto);
julia> transform(df, AsTable(Between(:x50, :x100)) => ByRow(mean) => :mean_50_100)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 7, 2021, 4:23pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/3 "2021-03-07T16:23:16Z")

</div>

Yea the standard way to do this is

```julia
transform!(df,["col1","col2","col3"] => ByRow(mean) => "meancol")

```

where you just need to update the strings to correspond to your list of columns and the name of your new column. The `AsTable` in the above answer is a handy shortcut instead of listing the column names 1 by 1.

---

<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:** [March 7, 2021, 4:28pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/4 "2021-03-07T16:28:15Z")

</div>

This will fail

```julia
julia> mean(1, 2, 3)
ERROR: MethodError: no method matching mean(::Int64, ::Int64, ::Int64)

```

You need the `AsTable` so that the input is a `NamedTuple`.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 7, 2021, 4:42pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/5 "2021-03-07T16:42:15Z")

</div>

You’re right! I guess that’s just what I _want_ to work. I think I made that mistake just a few days ago too…

EDIT: It is just a failing of the `mean` method though, not of the approach here.

---

<div class="post-metadata">

**Author:** ![korilium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korilium/32/48714_2.png) [@korilium](https://discourse.julialang.org/u/korilium)\
**Post date:** [March 7, 2021, 4:57pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/6 "2021-03-07T16:57:33Z")

</div>

Thank you for your responses!

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 7, 2021, 4:57pm UTC](https://discourse.julialang.org/t/dataframe-creating-a-rowmean-for-95-columns/56671/7 "2021-03-07T16:57:45Z")

</div>

For posterity I will add the fix to my earlier solution. Annoying that this is needed, but you can make it a tuple before passing to mean.

```julia
transform!(df,["col1","col2","col3"] => ByRow(mean) ∘ ByRow(tuple) => "meancol")

```
