# Adding rows

**URL:** https://discourse.julialang.org/t/adding-rows/49364
**Category:** New to Julia
**Tags:** question
**Created:** [October 31, 2020, 1:30pm UTC](https://discourse.julialang.org/t/adding-rows/49364 "2020-10-31T13:30:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![r.bhl](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@r.bhl](https://discourse.julialang.org/u/r.bhl)
#### Post date: [October 31, 2020, 1:30pm UTC](https://discourse.julialang.org/t/adding-rows/49364/1 "2020-10-31T13:30:37Z")

</div>

Hi there! I am a bit new to julia so I have a few a very basic questions:

I want to sum 33 rows of an nxm matrix. How can I do so?

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [October 31, 2020, 2:20pm UTC](https://discourse.julialang.org/t/adding-rows/49364/2 "2020-10-31T14:20:04Z")

</div>

Hi @r.bhl, welcome to the community! Your question is really about array indexing. Have a look at the manual here (you can skip straight to the indexing section if you wish):

[https://docs.julialang.org/en/v1/manual/arrays/](https://docs.julialang.org/en/v1/manual/arrays/)

and let us know if it’s still unclear or if you need further assistance.

---

<div class="post-metadata">

### Author: ![r.bhl](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@r.bhl](https://discourse.julialang.org/u/r.bhl)
#### Post date: [October 31, 2020, 3:53pm UTC](https://discourse.julialang.org/t/adding-rows/49364/3 "2020-10-31T15:53:45Z")

</div>

Hello @mthelm85! thank you for the reply!  
The question I have to answer: sort the 30 assets according to the sum of returns over the last 33 days. Assets are given in columns and days are the rows. Data is given for 10 years, and I have to (I think) create a loop for returns.

**My commands I have tried:**  
#R is the return which I indexed from a larger database.

m = 1  
n = R[1:30]  
for t = [1:33][1]  
local n  
m = m + t  
n = t  
R2 = sum(R[m])  
end  
printmat(R2)

**Another command I tried:**

Rsum = sum(R,dims=1)

How could I go about this?

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [October 31, 2020, 5:18pm UTC](https://discourse.julialang.org/t/adding-rows/49364/4 "2020-10-31T17:18:40Z")

</div>

You should always quote your code here with backticks ```, like this:

```julia
m = 1
n = R[1:30]
for t = [1:33][1]
local n
m = m + t
n = t
R2 = sum(R[m])
end
printmat(R2)

```

It makes it much easier to read 😀 Have a look at [this thread](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/18) when you get a chance.

I’m not sure I’m following but if you have some returns data in a `Matrix` R and then the names of the assets in a vector `assets`, you can do this:

```julia
# Create fake data
R = rand(Float64, (50,30))
assets = ["company$i" for i in 1:30]

rets = [(company=i[2], ret=sum(i[1][1:33])) for i in zip(eachcol(R), assets)]

julia> sorted_assets = [sort(rets, by=last, rev=true)[i][1] for i in 1:30]
30-element Array{String,1}:
 "company18"
 "company11"
 "company6"
 "company29"
 "company7"
 "company5"
 "company1"
 "company30"
 ⋮
 "company15"
 "company3"
 "company26"
 "company21"
 "company20"
 "company2"
 "company22"
 "company14"

```

That being said, I would probably use DataFrames.jl to do this kind of work. The documentation for that package is good and it will probably allow you to get up and running with these kinds of operations faster than if you just use base Julia.

---

<div class="post-metadata">

### Author: ![r.bhl](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@r.bhl](https://discourse.julialang.org/u/r.bhl)
#### Post date: [October 31, 2020, 10:16pm UTC](https://discourse.julialang.org/t/adding-rows/49364/5 "2020-10-31T22:16:42Z")

</div>

Thank you so much @mthelm85. Just a small doubt- the array we get of the 30 element string is a sorted function of their summed returns? If not, how could I rank these assets on the basis of their sum?

I can’t thank you enough. I am new to this language and amidst covid it’s become even tougher to seek guidance.

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [November 1, 2020, 12:34am UTC](https://discourse.julialang.org/t/adding-rows/49364/6 "2020-11-01T00:34:48Z")

</div>

Anytime! You’ll find that the community here is very helpful and on numerous occasions, I’ve been amazed at how much time & effort folks are willing to put in to help me out (so I do my best to pay it forward 😉). If you can provide a minimum working example (MWE), you’ll nearly always receive help that will allow you to solve your problem 🙂.

In the example above, if I just wanted an array of sorted returns, I would do something like this (this is much easier):

```julia
R = rand(Float64, (50,30))

julia> rets = sort([sum(col[1:33]) for col in eachcol(R)], rev=true)
30-element Array{Float64,1}:
 20.547714995916056
 20.0038231068496
 19.6576605056632
 19.412501859347227
 19.170970130142976
 18.272165423676235
 18.25366634943681
 17.822982398212552
 17.76791261284992
  ⋮
 15.134177106423126
 15.098637341222217
 14.878716664527401
 14.833317877379049
 14.486749764568177
 14.455917909240188
 14.183262764805674
 14.117427894274988

```

To fully understand the above, check out the following sections of the manual:

- [array construction/initialization](https://docs.julialang.org/en/v1/manual/arrays/#Construction-and-Initialization)
- [array comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions)
- [generator expressions](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions)

Also, note that you can type `?` in the REPL and then search for function names, types, keywords, etc. So, if you want to understand the `sort`, `rand` and `eachcol` functions, in your Julia REPL, just type `?` and you’ll see it change from `julia>` to `help?>`. Then, you’ll see something like this:

```julia
help?> rand
search: rand randn transcode macroexpand @macroexpand @macroexpand1 CartesianIndex CartesianIndices range transpose LinRange UnitRange StepRange

  rand([rng=GLOBAL_RNG], [S], [dims...])

.
. # more info here
.

```
